首页 > 代码库 > 接口练习
接口练习
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("我会黯然销魂掌"); }}
接口练习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。