类名作为形式参数和返回值(附CSDN上跳转指定文段位置代码)

点击跳转到相应位置

跳转至此位置

类名作为形式参数

要的其实是该类的对象

package Object;

public class Student001 {
    
    
    public void study(){
    
    
        System.out.println("一天不学习,马上变垃圾!");
    }
}
//---------------------------------------------------------------------------------------
package Object;

public class Student002 {
    
    
    public void test(Student001 s){
    
    
        s.study();
    }
}
//---------------------------------------------------------------------------------------
package Object;
/*
 * 需求:调用Student002中的test方法
 * 
 * 类型作为形式参数:其实需要的是该类对象
 */
public class Student003 {
    
    
    public static void main(String[] args) {
    
    
        Student002 t=new Student002();
        Student001 s=new Student001();
        t.test(s);
    }
}

类名作为返回值

返回的其实是该类的对象

package Object;

public class Student {
    
    
	public void study() {
    
    
		System.out.println("一天不学习,马上变垃圾");
	}
}
//---------------------------------------------------------------------------------------
package Object;
public class Teacher {
    
    
	public Student getStudent() {
    
    
		Student s = new Student();
		return s;		
	}
}
//---------------------------------------------------------------------------------------
package Object;
/*
 * 需求:调用Teacher中的test方法
 * 
 * 类型作为返回值:返回的是该类对象
 */
public class Test {
    
    
	public static void main(String[] args) {
    
    
		Teacher t = new Teacher();
		Student s = t.getStudent();
		s.study();
	}
}

猜你喜欢

转载自blog.csdn.net/m0_52646273/article/details/114082539