package hanqi; public interface CanSwim { void swim(); }
package hanqi; public interface CanFly { public void fly(); }
package hanqi; public abstract class ActionCharacter { abstract void fight(String emp); abstract void speak(String s); }
package hanqi; public class Hero extends ActionCharacter implements CanFly, CanSwim{ String name; Hero(String name) { this.name=name; } public void swim() { System.out.println(this.name+",游泳"); } public void fly() { System.out.println(this.name+",飞"); } void fight(String emp) { System.out.println(this.name+","+emp); } void speak(String s) { System.out.println(this.name+","+s); } }
package hanqi; public class Adventure { public static void main(String[] args) { // TODO 自动生成的方法存根 Hero hb = new Hero("A"); hb.swim(); hb.fight("B"); hb.fly(); CanFly cf = hb; hb.fly(); CanSwim cs = hb; hb.swim(); ActionCharacter ac = hb; ac.speak("C"); ac.fight("D"); } }
Java基础-接口看下图实现如下接口和类,并完成Adventure中的主方法
原文:http://www.cnblogs.com/zs6666/p/5911194.html