接口4
1 public interface CanSwim { 2 3 void Swim(); 4 }
1 public interface CanFly { 2 3 void Fly(); 4 }
1 public class Hero extends ActionCharacter implements CanSwim, CanFly { 2 3 String name; 4 5 Hero(String name) { 6 this.name = name; 7 } 8 9 @Override 10 public void Fly() { 11 System.out.println(this.name + "会飞。"); 12 13 } 14 15 @Override 16 public void Swim() { 17 System.out.println(this.name + "会游泳。"); 18 19 } 20 21 }
1 public class Adventure { 2 3 public static void main(String[] args) { 4 Hero hb = new Hero("张三"); 5 hb.Swim(); 6 hb.fight(""); 7 hb.Fly(); 8 9 CanFly cf = new Hero("李四"); 10 cf = hb; 11 cf.Fly(); 12 13 CanSwim cs = new Hero("王五"); 14 cs = hb; 15 cs.Swim(); 16 17 ActionCharacter ac = new Hero("马六"); 18 ac = hb; 19 ac.speak("大家跟我一起战斗!"); 20 ac.fight("发射!"); 21 } 22 23 }
结果: