zoukankan      html  css  js  c++  java
  • 接口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 }

    结果:

  • 相关阅读:
    生成器
    迭代器
    闭包函数
    装饰器(2)
    装饰器(1)
    名称空间与作用域(2)
    110.网络编程-mail
    109.网络编程-FTP
    108.网络编程-TCP/UDP
    107.xpath
  • 原文地址:https://www.cnblogs.com/ouyangtangfeng99/p/5523346.html
Copyright © 2011-2022 走看看