package test10;
class student{
public void study(){
System.out.println("study");
}
}
class studentDemo{
public student getStudy(){
return new student();
}
}
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//通过studentDemo来调用方法
studentDemo d=new studentDemo();
student x=d.getStudy();//相当于new student
x.study();
}
}
抽象类:返回的该抽象类的子类对象
package test10;
abstract class person{
public abstract void study();
}
class personDemo{
public person getPerson(){
return new student();//相当于person p=new student();
}
}
class student extends person{
public void study(){
System.out.println("study");
}
}
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//通过personDemo来调用方法
personDemo d=new personDemo();
person p=d.getPerson();//相当于person p=new student();多态的方法
p.study();
}
}
接口:返回的是该接口实现类的对象
package test10;
interface person{
public abstract void study();
}
class personDemo{
public person getPerson(){
return new student();//相当于person p=new student();
}
}
//定义具体类来实现接口
class student implements person{
public void study(){
System.out.println("study");
}
}
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//通过personDemo来调用方法
personDemo d=new personDemo();
person p=d.getPerson();//相当于new student();运用多态的方法实现具体类
p.study();
}
}