Interface实现多态简例
2019獨角獸企業重金招聘Python工程師標準>>>
Realization in Java:
public interface Food(){
???????? public void isEaten();
}
class Dish implements Food {
???????? public String name;
???????? public Dish(String name){
???????? this.name=name;
}
public void isEaten(){
???????????System.out.println("Food "+name+" is eaten. ");
}
}
class Guest{?????????????????????????????????????? //?無需實現接口,只需要利用接口名創建對象。?? <<use>>
????????? public Food lunch;???????????????? //??間接使用到了class Dish的constructor?和?attribute.?實現了多態性 polymorphism
????????? public Guest(){
????????????? lunch= new Dish("Spaghetti");
}
}
}
轉載于:https://my.oschina.net/u/1409824/blog/286058
總結
以上是生活随笔為你收集整理的Interface实现多态简例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery.mobile手机网页简要
- 下一篇: openStack工具集