Java 面向接口的

面向接口编程和面向实现的编程是区别

万物皆接口确实吊!

看代码!

创建接口类

public interface IPaper {
    String getContext();
}

常见纸实现类

public class MyBook implements  IPaper{
    public String getContext() {
        return "这是图书内容";
    }
}
public class Newspaper implements IPaper{
    public String getContext() {
       return "这是报纸内容";
    }
}

创建妈妈

public class Mother {
    public  void read(IPaper paper){
        System.out.println(paper.getContext());
    }
}

调用

public class Main {
    public static void main(String[] args) {
        Mother mother = new Mother();
        mother.read(new MyBook());
        mother.read(new Newspaper());
    }
}

爽不爽!

posted @ 2020-11-17 09:48  Ivin-yang  阅读(73)  评论(0编辑  收藏  举报