接口回调:
接口回调是指
:可以把使用某一接口的类创建的对象的引用赋给该接口声明的接口变量,那么该接口变量就可以调用被类实现的接口的方法。实际上,当接口变量调用被类实现的接口中的方法时,就是通知相应的对象调用接口的方法,这一过程称为对象功能的接口回调。看下面示例。
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.
java中的接口的回调?
我想问一下:"接口回调"用通俗的话怎么理解???怎么理解它是多态的另一种形式???
具体怎么解释可能不是特别好解释啊....给你看看刚写的一段程序好了...很容易理解的...
interface Msg{
void showMessage();
}
class NbMsg implements Msg{
public void showMessage() {
System.out.println("123456");
}
}
class CahrMsg implements Msg{
public void showMessage() {
System.out.println("abcdef");
}
}
public class test {
private Msg msg;
public void show() {
System.out.println("处理一些东西");
msg.showMessage();
}
public void setMsg(Msg msg) {
this.msg = msg;
}
public static void main(String[] args) {
test a = new test();
a.setMsg(new CahrMsg());
a.show();
a.setMsg(new NbMsg());
a.show();
}
}
结果是:
处理一些东西
abcdef
处理一些东西
123456