首页 > 编程语言 > 详细

java通过继承来扩展接口

时间:2014-06-10 09:00:57      阅读:306      评论:0      收藏:0      [点我收藏+]

接口主要是为了向上转型,从而使基类(接口方法)更具灵活性!不想废话,看例子:

bubuko.com,布布扣
/**
 * 
 */
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);
    }

}
bubuko.com,布布扣
结果
bubuko.com,布布扣
DangerousMonster menace()
DangerousMonster menace()
DangerousMonster destory()
VeryBadVampire menace()
VeryBadVampire menace()
VeryBadVampire destroy()
VeryBadVampire kill()
bubuko.com,布布扣

java通过继承来扩展接口,布布扣,bubuko.com

java通过继承来扩展接口

原文:http://www.cnblogs.com/fxyfirst/p/3778629.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!