ivin

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

万物皆接口确实吊!

看代码!

创建接口类

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());
    }
}

爽不爽!

分类:

技术点:

相关文章:

  • 2021-06-24
  • 2021-07-25
  • 2021-10-19
  • 2021-10-29
  • 2022-01-19
  • 2021-12-29
  • 2021-12-29
猜你喜欢
  • 2021-10-29
  • 2021-10-29
  • 2021-10-29
  • 2021-11-08
  • 2021-10-19
  • 2021-10-19
  • 2021-11-30
相关资源
相似解决方案