1、类名作为方法的形参

    方法的形参是类名,其实需要的是该类的对象

    1. 实际传递的是该对象的【地址值】
    1. public class Teacher{
    2. private String name;
    3. private int age;
    4. public void method(Student stu){
    5. stu.study();
    6. }
    7. }
    8. public class Student{
    9. private String name;
    10. private inr age;
    11. public void study(){}
    12. }
    13. public class Test(){
    14. public static void main(String[] args){
    15. Teacher teacher = new Teacher();
    16. Student stu = new Student();
    17. teacher.method(stu)
    18. }
    19. }

    2、类名作为方法的返回值

    1. 方法的返回值是类名,其实返回的是该类的对象
    2. 实际传递的,也是该对象的【地址值】
    1. public class Student{
    2. private String name;
    3. private int age;
    4. public void study(){
    5. ....
    6. }
    7. }
    8. public class Teacher{
    9. private String name;
    10. private int age;
    11. public Student getStudent(){
    12. Student stu = new Student();
    13. return stu;
    14. }
    15. }
    16. public class Test{
    17. public static void main(String[] args){
    18. Teacher teacher = new Teacher();
    19. Student stu = teacher.getStudent();
    20. stu.study();
    21. }
    22. }