本文共 966 字,大约阅读时间需要 3 分钟。
1.1 类名作为方法的形式参数
1.1.1 案例代码十四:package com.itheima_10;public class Student {public void study() {System.out.println("好好学习,天天向上");}}package com.itheima_10;public class Teacher {public void test(Student s) {//接收传递过来的Student对象的地址值s.study(); }}package com.itheima_10;//需求: 调用Teacher的test方法//类名作为形式参数:其实这里需要的是该类对象。public class Test {public static void main(String[] args) {Teacher t = new Teacher();Student s = new Student();t.test(s);}}
1.2 类名作为返回值案例
1.2.1 案例代码十五:package com.itheima_11;public class Student {public void study() {System.out.println("好好学习,天天向上");}}package com.itheima_11;public class Teacher {public Student getStudent() {Student s = new Student();return s;//返回的是Student对象的地址值}}
package com.itheima_11;//需求: 通过Teacher得到Student对象,然后调用Student类的方法//如果方法的返回值是类名:其实返回的是该类的对象public class Test {public static void main(String[] args) {Teacher t = new Teacher();Student s = t.getStudent();s.study();}}
转载于:https://blog.51cto.com/13517854/2087547