类名作为返回值类型


Student.java

public class Student {
    public void study()
    {
        System.out.println("好好学习,天天向上");
    }

}

 

 

Teacher.java

public class Teacher {
    //Student是学生类名
    public Student getStudent()
    {
        Student s=new Student();
        return s;
    }
}

 

 

Test.java

//需求:通过Teacher得到Student对象,然后调用Student类的方法
//如果方法的返回值是类名:其实返回的该类的对象
public class Test {
    public static void main(String[] args) {
        Teacher t=new Teacher();
        Student s=t.getStudent();
        s.study();
    }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM