集册 Java实例教程 声明接口类型的数组

声明接口类型的数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

751
声明接口类型的数组

interface Walkable {

  void walk();

}
/*来 自 N o w J a v a . c o m - 时  代  Java*/

class Person implements Walkable {

  private String name;


  public Person(String name) {

    this.name = name;

  }


  public void walk() {

    System.out.println(name + " (a person) is walking.");

  }

}


class Duck implements Walkable {

  private String name;


  public Duck(String name) {
  /** 
  来 自 
  N o w J a v a . c o m - 时  代  Java
  **/

    this.name = name;

  }


  public void walk() {

    System.out.println(name + " (a duck) is walking.");

  }

}


class Walkables {

  public static void letThemWalk(Walkable[] list) {

    for (Walkable w : list) {

      w.walk();

    }

  }

}


public class Main {

  public static void main(String[] args) {

    Walkable[] w = new Walkab
展开阅读全文