// <1>类作为返回值
class Student {
publicvoidstudy() {
System.out.println("Good Good Study,Day Day Up");
}
}
class StudentDemo {
public Student getStudent() {
//Student s = new Student();//return s;returnnew Student();
}
}
class StudentTest2 {
publicstaticvoidmain(String[] args) {
StudentDemo sd = new StudentDemo();
Student s = sd.getStudent(); //new Student(); Student s = new Student();
s.study();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
抽象类作为返回值-代码块
// <2> 抽象类作为返回值abstractclassPerson {publicabstractvoid study();
}
classPersonDemo {public Person getPerson() {
//Person p = new Student();//return p;returnnew Student();
}
}
classStudentextendsPerson {publicvoid study() {
System.out.println("Good Good Study,Day Day Up");
}
}
classPersonTest2 {publicstaticvoid main(String[] args) {
//需求:我要测试Person类中的study()方法
PersonDemo pd = new PersonDemo();
Person p = pd.getPerson(); //new Student(); Person p = new Student(); 多态
p.study();
}
}