class Book{
public String getContent(){
return "很久很久以前有一个阿拉伯的故事……"; }
}
class Newspaper{
public String getContent(){
return "林书豪38+7领导尼克斯击败湖人……";}
}
class Mother{
public void narrate(Newspaper newspaper){
System.out.println("妈妈开始讲故事");
System.out.println(Book.getContent());
System.out.println(Newspaper.getContent());
}
}
public class Client{
public static void main(String[] args){
Mother mother = new Mother();
mother.narrate(new Reader());
}
}
按照这个思路修改dai'ma
就是写一个IReader的接口类,里面只有getContent()方法,然后Book和newspaper都实现这个接口,调用的时候可以是
IReader reader = new Book();
mather.naarrate(reader);
https://blog.csdn.net/weixin_34356138/article/details/85590529