类名作为形参和返回值

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/13587708/2084080

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值