通过接口,我们将大量共通但实现细节不同的方法抽象出来,再去实现它的接口类中具体处理,这样通过接口去调用方法的时候,就不用考虑具体调用哪个方法了。
interface People{
void peopleList();
}
class Student implements People{
public void peopleList(){
System.out.println("I’m a student.");
}
}
class Teacher implements People{
public void peopleList(){
System.out.println("I’m a teacher.");
}
}
public class Example{
public static void main(String args[]){
People a; //声明接口变量
a=new Student(); //实例化,接口变量中存放对象的引用
a.peopleList(); //接口回
a=new Teacher(); //实例化,接口变量中存放对象的引用
a.peopleList(); //接口回调
}
}
博客介绍了接口的作用,通过接口可将大量共通但实现细节不同的方法抽象出来,在实现接口的类中具体处理,调用方法时无需考虑具体调用哪个方法,体现了接口在编程中的便利性。
549

被折叠的 条评论
为什么被折叠?



