集册 Java实例教程 证明接口变量可以存储实现接口的类的对象的引用

证明接口变量可以存储实现接口的类的对象的引用

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

524
证明接口变量可以存储实现接口的类的对象的引用
/*时代Java - N o w  J a v a . c o m 提供*/

interface Swimmable {

  void swim();

}


class Fish implements Swimmable {

  private String name;


  public Fish(String name) {

    this.name = name;

  }


  public void swim() {

    System.out.println(name + " (a fish) is swimming.");

  }

}


public class Main {

  public static void main(String[] args) {

    Swimmable finny = new Fish("Finny");

    finny.swim();/**来自 N o w  J a v a  . c o m**/

  }

}