Java中接口回调

日期:2020/1/14

功能:实现java中的接口回调

IDE:Intellij IDEA

接口回调类似之前的上转型对象,接口回调表示接口的实例化变量存放实现该接口的类的对象的引用

接口回调可以让接口的实例化变量调用实现该接口的自定义类中的方法

package testDemo;

interface People{
    void peopleList();
}

class StudentPeople implements People{
    @Override
    public void peopleList() {
        System.out.println("我是学生");
    }
}

class TeacherPeople implements People{

    @Override
    public void peopleList() {
        System.out.println("我是老师");
    }
}


public class BackInterface {
    public static void main(String[] args){
        People people;
        people = new StudentPeople();
        people.peopleList();
        people = new TeacherPeople();
        people.peopleList();
    }
}

发布了76 篇原创文章 · 获赞 2 · 访问量 2133

猜你喜欢

转载自blog.csdn.net/weixin_43476969/article/details/103979784