编码实现动物世界的继承关系:
a) 动物(Animal)具有行为:吃(eat)、睡觉(sleep) b) 受保护得物件(接口)具有得行为:beProtected(); c) 有价值得物件(接口)具有得行为:getMoney() d) 老虎是动物,同时是有价值且受保护得动物类型。 e) 熊猫是动物,同时是有价值且受保护得动物类型。 f) 这些动物吃的行为各不相同(熊猫吃竹子,老虎吃肉);但睡觉的行为是一致的;受保护得级别不一致,价值也不一致。
g) 请通过继承,接口实现以上需求,并编写测试类AnimalTest进行测试。
Animail.java父类-动物类
package com; /**
* @author Administrator * */
public class Animal { private String name; private String food; //吃的一个行为 public void eat() throws Exception{ System.out.println(this.name+"吃"+this.food+"!"); }
//睡觉的一个行为 public void sleep(){ System.out.println(this.name+"在睡觉!"); }
public String getName()
{
return name; }
public void setName(String name) { this.name = name; }
public String getFood() { return food; }
public void setFood(String food) { this.food = food; }
}
AnimalInterface.Java 接口
package com;
public interface AnimalInterface { //默认为public,因为接口要被实现,并被广范使用,
//所以最好是public因为如果不是公开的就没有必要做接口了 void beProtected(); void getMoney();
}
AnimalTest.java 测试类
package com;
public class AnimalTest { /**
* @param args
* @throws Exception */
public static void main(String[] args) throws Exception {
//当一种动物创造出来的时候,就会有一些行为 因为extends(继承)了Animal(动物)的特征 Tiger t= new Tiger(); Panda p= new Panda(); System.out.println("-------分割线--------");
//这是继承,当一个动物被生成的时候,就已经被赋予了类型和食物 t.eat();//饿了就得吃 t.sleep();//困了就得睡
p.eat();//不管什么动物都是一样的 p.sleep();//所以也要睡觉
//动物都有许多相同点,比如老虎和熊猫,它们是受保护的,和有价值的
//有些是必须的,所以implement(实现)了 AnimalInterface(动物界的一些特征-一个接口) t.beProtected();
p.beProtected();
System.out.println("-------分割线--------"); //如果觉得没有这些特征,那么可以不实现 Animal m= new Mouse();//真是悲哀,没有特权,因为老爹不是李刚所以没有保护,也没有价值,不过有的行为还是有的只是有点不同,像下面一样 m.eat();//因为重写了eat方法(环境所迫)所以它吃的行为有点与众不同 System.out.println("-------分割线--------"); //这是继承的第二种表现形式 Animal tt=new Tiger(); tt.eat(); Animal pp=new Panda(); pp.eat(); System.out.println("-------分割线--------"); //实现接口 //由父类造一个子类 AnimalInterface at = new Tiger(); AnimalInterface ap = new Panda(); System.out.println("-------分割线--------"); at.beProtected(); at.getMoney(); ap.beProtected(); ap.getMoney(); } }
Panda.java熊猫类,继承了父类,并实现了接口
package com;
public class Panda extends Animal implements AnimalInterface { public Panda() throws Exception{ this.setName("熊猫"); this.setFood("竹子"); super.eat(); beProtected(); getMoney(); }
@Override
public void beProtected() { System.out.println(this.getName()+"是受保护的动物类型"); }
@Override
public void getMoney() { System.out.println(this.getName()+"是 有价值的动物类型");
}
}
Tiger.java 老虎类,同熊猫类
package com;
public class Tiger extends Animal implements AnimalInterface { public Tiger()throws Exception{ this.setName("老虎");
this.setFood("肉"); super.eat(); beProtected(); getMoney();
}
@Override
public void beProtected() { System.out.println(this.getName()+"是受保护的动物类型");
}
@Override
public void getMoney() { System.out.println(this.getName()+"是 有价值的动物类型"); }
}
Mouse.java 老鼠类,只是继承父类,并没有实现接口(因为在大多数人看来,老鼠不被人保护,而且没有利用价值)
package com;
public class Mouse extends Animal { public Mouse() throws Exception{ //因为它常常躲在地下所以可以private this.setName("老鼠"); this.setFood("残羹冷炙!"); super.eat(); } @Override public void eat(){ System.out.println(this.getName()+"躲着吃"+this.getFood()); } }