首页 > 代码库 > java通过继承来扩展接口
java通过继承来扩展接口
接口主要是为了向上转型,从而使基类(接口方法)更具灵活性!不想废话,看例子:
/** * */ package interfaces; interface Monster{ void menace(); } interface DangerousMonster extends Monster{ void destory(); } interface Lethal{ void kill(); } class DragonZilla implements DangerousMonster{ public void menace(){ System.out.println("DangerousMonster menace()"); } public void destory(){ System.out.println("DangerousMonster destory()"); } } interface Vampire extends DangerousMonster,Lethal{ void drinkBlood(); } class VeryBadVampire implements Vampire{ public void menace(){ System.out.println("VeryBadVampire menace()"); } public void destory() { // TODO Auto-generated method stub System.out.println("VeryBadVampire destroy()"); } public void kill() { // TODO Auto-generated method stub System.out.println("VeryBadVampire kill()"); } public void drinkBlood() { System.out.println("VeryBadVampire dinrkBlood()"); } } public class HorrorShow { static void u(Monster b){b.menace();} static void v(DangerousMonster d){ d.menace(); d.destory(); } static void w(Lethal l){ l.kill(); } public static void main(String[] args) { // TODO Auto-generated method stub DangerousMonster barney = new DragonZilla(); u(barney); v(barney); Vampire Vlad = new VeryBadVampire(); u(Vlad); v(Vlad); w(Vlad); } }
DangerousMonster menace()
DangerousMonster menace()
DangerousMonster destory()
VeryBadVampire menace()
VeryBadVampire menace()
VeryBadVampire destroy()
VeryBadVampire kill()
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。