1、类名作为方法的形参
方法的形参是类名,其实需要的是该类的对象
实际传递的是该对象的【地址值】
public class Teacher{
private String name;
private int age;
public void method(Student stu){
stu.study();
}
}
public class Student{
private String name;
private inr age;
public void study(){}
}
public class Test(){
public static void main(String[] args){
Teacher teacher = new Teacher();
Student stu = new Student();
teacher.method(stu)
}
}
2、类名作为方法的返回值
方法的返回值是类名,其实返回的是该类的对象
实际传递的,也是该对象的【地址值】
public class Student{
private String name;
private int age;
public void study(){
....
}
}
public class Teacher{
private String name;
private int age;
public Student getStudent(){
Student stu = new Student();
return stu;
}
}
public class Test{
public static void main(String[] args){
Teacher teacher = new Teacher();
Student stu = teacher.getStudent();
stu.study();
}
}