标签:
package pack1; public interface CanFly { void fly(); }
package pack1; public interface CanSwim { void swim(); }
package pack1; public abstract class ActionCharacter { abstract void fight(String emp); void speak(String s) { System.out.println(s); } }
package pack1; public class Hero extends ActionCharacter implements CanSwim, CanFly { String name; public Hero(String name) { super(); this.name = name; } @Override public void fly() { System.out.println(this.name+"会飞"); } @Override public void swim() { System.out.println(this.name+"会游泳"); } @Override void fight(String emp) { System.out.println(this.name+"打架很厉害"); } }
package pack1; public class Adventure { public static void main(String[] args) { Hero hb=new Hero("超人"); hb.swim(); hb.fly(); hb.fight(null); CanFly cf=hb; cf.fly(); CanSwim cs=hb; cs.swim(); ActionCharacter ac=hb; ac.fight("超人"); ac.speak("你好"); } }
java接口练习:看下图实现如下接口和类,并完成Adventure中的主方法。
标签:
原文地址:http://www.cnblogs.com/wallan/p/5523187.html