首页 > 代码库 > 接口练习

接口练习

技术分享

技术分享

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("我会黯然销魂掌");    }}

技术分享

 

接口练习