直接看代码:
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();
}
}
输出:
I’m a student. I’m a teacher.
有点绕口:接口回调可以把使用某一接口的类创建的对象的引用赋给该接口声明的接口变量,那么该接口变量就可以调用被类实现的接口的方法。