日期:2014-05-20  浏览次数:20744 次

帮帮我,谢谢
class Bird extends Animal implements Pet {
  private String name;
  public Bird() {
  ///super(2); // 这行是必需的

  }
  /*如果Bird无显式构造器,系统将调用却省无参构造器Bird(),而Bird()将调用父类无参构造器,
  * Animal不存在无参构造器,因此会出错
  *如果Animal有无参构造器,则不为Bird写构造器,或者写Bird()均可以*/
  public void setName(String name) {
  this.name = name;
  }
  public String getName() {
  return name;
  }
  public void speak() {
  System.out.println("speak:Tweedle");
  }
  public void play() {
  System.out.println("play:Birds fly on the sky all day.");
  }

//Bird 覆盖父类Animal 的方法walk()
  public void walk() {
  //super.walk();
  System.out.println("walk:Birds, of course, can't walk; they fly .");
  }

//Bird 覆盖父类Animal 的抽象方法eat()
  public void eat() {
  System.out.println("eat:Birds eat corn .");
  }

//Bird 创建自己的方法buildNest() 
  public void buildNest() { 
  System.out.println("buildNest: Birds buid the nest within the branches.");
}

//Bird 创建自己的方法layEggs()
  public void layEggs() {
  System.out.println("layEggs: Birds lay eggs.");
}
}
abstract class Animal
{
protected int legs;
protected Animal(int legs)
{
this.legs = legs; 
}
protected Animal()
{

}
  public abstract void eat();
  public void walk() {
  System.out.println("This animal walks on " + legs + " legs.");
  }
}
 class Cat extends Animal implements Pet {
  private String name;
  public Cat(String name) {
  super(4);
  this.name = name;
  }
  public Cat() {
  this("");
  }

  public void setName(String name) {
  this.name = name;
  }
  public String getName() {
  return name;
  }
  public void speak() {
  System.out.println("speak:Meow");
  }
  public void play() {
  System.out.println("play:"+name + " likes to play with string.");
  }

//Cat 覆盖父类Animal 的抽象方法eat() 
public void eat() {
  System.out.println("eat:Cats like to eat spiders and mice.");
  }
}
  
   
   
  interface Pet {
  public abstract void setName(String name);
  public abstract String getName();
  public abstract void speak();
  public abstract void play();
}
 class Spider extends Animal {
  public Spider() {
  super(8);
  }

//Spider 覆盖父类Animal 的抽象方法eat()
  public void eat() {
  System.out.println("eat:Spiders catch flies in their webs to eat.");
  }
}
public class tAnimal {
  public static void main(String[] args) {
  Bird b = new Bird();
  Cat c = new Cat("Fluffy");
  Animal ab = new Bird();
  Animal as = new Spider();
  Pet p = new Cat(); //允许定义接口类型的变量
  //示例对接口的不同实现
b.speak(); //调用Bird类对接口Pet的实现
b.play(); //调用Bird类对接口Pet的实现
b.eat();
b.walk();
p.speak(); //通过接口类型的变量p访问Cat实现的接口方法
c.play(); //调用Cat类对接口Pet的实现
c.eat();
c.walk();
 as.eat(); 
  as.walk(); 
  ((Bird)ab).speak();
ab.walk();  
  }
}
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)