19.类名作为返回值类型

类名作为返回值案例

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();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容