我在前面的文章里就已经说过,Java不支持多重继续.但它确实答应一个对象通过使用叫做"接口"的功能拥有多个特性.下面的例子显示了定义LandAnimal的接口的可能的定义代码: public interface LandAnimal { public int getNumberOfLegs(); public boolean hasATail(); }
public String getHello(); public String getHello(int mood);
现在,让我们修改例子程序来将我们讨论的一些概念付诸实践:
public class HelloWorld { public static void main(String[] args) { Dog animal1 = new Dog(); Cat animal2 = new Cat(); DUCk animal3 = new Duck();
System.out.PRintln("A dog says " +animal1.getHello() +", when scared says: " +animal1.getHello(Animal.SCARED) +", is carnivorous: " +animal1.isCarnivorous() +", is a mammal: " +animal1.isAMammal()); System.out.println("A cat says " +animal2.getHello() +", when comforted says: " +animal2.getHello(Animal.COMFORTED) +", is carnivorous: " +animal2.isCarnivorous() +", is a mammal: " +animal2.isAMammal()); System.out.println("A duck says " +animal3.getHello() +", when scared says: " +animal3.getHello(Animal.SCARED) +", is carnivorous: " +animal3.isCarnivorous() +", is a mammal: " +animal3.isAMammal()); } }
abstract class Animal { public static final int SCARED = 1; public static final int COMFORTED = 2;
public boolean isAMammal() { return(true); }
public boolean isCarnivorous() { return(true); }
abstract public String getHello(); abstract public String getHello(int mood); }
interface LandAnimal { public int getNumberOfLegs(); public boolean hasATail(); }
interface WaterAnimal { public boolean hasGills(); public boolean laysEggs(); }
class Dog extends Animal implements LandAnimal { // 重载父类的方法 public String getHello() { return("Bark"); }
public String getHello(int mood) { switch (mood) { case SCARED: return("Growl"); case COMFORTED: return(""); }
return("Bark"); }
// LandAnimal 接口的实现 public int getNumberOfLegs() { return(4); }
public boolean hasATail() { return(true); } }
class Cat extends Animal implements LandAnimal { // 重载父类的方法 public String getHello() { return("Meow"); }
public String getHello(int mood) { switch (mood) { case SCARED: return("Hiss"); case COMFORTED: return("Purr"); }
return("Meow"); }
// LandAnimal 接口实现 public int getNumberOfLegs() { return(4); }
public boolean hasATail() { return(true); } }
class Duck extends Animal implements LandAnimal, WaterAnimal { // 重载父类的方法 public String getHello() { return("Quack"); }
public String getHello(int mood) { switch (mood) { case SCARED: return("Quack, Quack, Quack"); case COMFORTED: return(""); }
return("Quack"); }
public boolean isAMammal() { return(false); }
public boolean isCarnivorous() { return(false); }
// WaterAnimal 接口实现 public boolean hasGills() { return(false); }
public boolean laysEggs() { return(true); }
// LandAnimal 接口实现 public int getNumberOfLegs() { return(2); }
public boolean hasATail() { return(false); } }
程序执行后输出的结果如下:
A dog says Bark, when scared says: Growl, is carnivorous: true, is a mammal: true A cat says Meow, when comforted says: Purr, is carnivorous: true, is a mammal: true A duck says Quack, when scared says: Quack, Quack, Quack, is carnivorous: false, is a mammal: false