码迷,mamicode.com
首页 > 编程语言 > 详细

java通过继承来扩展接口

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

标签:des   c   style   class   blog   code   

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

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通过继承来扩展接口

标签:des   c   style   class   blog   code   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!