码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA接口的简单实现

时间:2016-04-23 13:13:42      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

感觉越来越强烈,

我的心在跳~~~,我的宝剑在嘟~~

 

Flyer.java

package cc.openhome;

public interface Flyer{
    public abstract void fly();
}

Swimmer.java

package cc.openhome;

public interface Swimmer{
  public abstract void swim();
}

Fish.java

package cc.openhome;

public abstract class Fish implements Swimmer {
  protected String name;
  public Fish(String name){
    this.name = name;
  }
  public String getName() {
    return name;
  }

  @Override
  public abstract void swim();
}

 

FlyingFish.java

package cc.openhome;

public class FlyingFish extends Fish implements Flyer {
    public FlyingFish(String name){
        super(name);
    }

    @Override
    public void swim(){
        System.out.println("FlyingFish swim");
    }

    @Override
    public void fly(){
        System.out.println("FlyingFish fly");
    }
}

SeaPlane.java

package cc.openhome;

public class Seaplane implements Swimmer, Flyer {
  private String name;

  public Seaplane(String name){
    this.name = name;
  }

  @Override
  public void fly() {
    System.out.printf("Seaplane %s is fly", name);
  }

  @Override
  public void swim(){
    System.out.printf("Seaplane %s is swim", name);
  }
}

Ocean.java

package cc.openhome;

public class Ocean {
  public static void main(String[] args){
    doSwim(new Seaplane("AirFoce Zero"));
    doSwim(new FlyingFish("Flying Ping"));
  }
    static void doSwim(Swimmer swimmer){
        swimmer.swim();
    }
}

技术分享

JAVA接口的简单实现

标签:

原文地址:http://www.cnblogs.com/aguncn/p/5424400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!