接口的使用

/*

 *接口Runner

*/

interface Runner{

      int ID=1;       //默认为public static final 标识

      void run();      //默认为public 标识

}

/*

*接口Animal, 继承自Runner

*/

interface Animal extends Runner{

      void breathe();      //默认为public标识

}

/*

* 抽象类LandAnimal,实现接口中的部分方法

*/

abstract class LandAnimal implements Animal{

      public void breathe(){

            System.out.println("LandAnimal is breathing");

      }

}

/*

*接口的实现类fish

*/

class Fish implements Animal{

      private double weight;

      public void run(){

            System.out.println("fish is swimming");

      }

      public void breathe(){

             System.out.println("fish is bubbling");     

       }

      public Fish(double weight){

            this.weight =weight;

      }

      public String toString(){

            return "this fish weiht :"+weight+"kg";

      }

}

 

/*

*测试类Zoo

*/

public class Zoo{

      public static void main(Sting[] agrs){

            Runner r=new Fish(5,8);

            Animal a=new Fish(3.06);

                  System.out.println(r);      //调用toSting

                  System.out.println(a);      //调用toSting

      }

}

posted @ 2009-04-14 19:01  kanjc  阅读(172)  评论(0编辑  收藏  举报