码迷,mamicode.com
首页 > 其他好文 > 详细

接口练习

时间:2016-09-24 09:13:32      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

技术分享

package com.homework4;

public abstract class ActionCharacter {
    
    //声明抽象方法
    abstract void fight(String emp);
    
    abstract void speak(String s);


}
package com.homework4;

public interface CanFly {
    
    //声明抽象方法
    void fly();

}
package com.homework4;

public interface CanSwim {
    
    //声明抽象方法
    void swim();

}
package com.homework4;

public class Hero extends ActionCharacter implements CanFly, CanSwim {
    
    String name;
    Hero(String name)
    {
        this.name=name;
    }

    @Override
    public void swim() {
        System.out.println("我是"+this.name+",我会游泳");

    }

    @Override
    public void fly() {
        System.out.println("我是"+this.name+",我会飞");

    }

    @Override
    void fight(String emp) {
        System.out.println("我是"+this.name+","+emp);

    }

    @Override
    void speak(String s) {
        System.out.println("我是"+this.name+","+s);

    }

}
package com.homework4;

public class E {

    public static void main(String[] args) {
        
        Hero hb = new Hero("张伟");
        hb.swim();
        hb.fly();
        hb.fight("我会降龙十八掌");
        
        
        CanFly cf = hb;
        hb.fly();
        
        CanSwim cs = hb;
        hb.swim();
        
        
        ActionCharacter ac = hb;
        ac.speak("我会说一口流利的汉语");
        ac.fight("我会黯然销魂掌");

    }

}

技术分享

 

接口练习

标签:

原文地址:http://www.cnblogs.com/HRZJ/p/5902398.html

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