///Animal类
package d922A;
public class Animal {
private String kind;
public String getKind() {
System.out.println(kind);
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public void eat()
{
System.out.println("I can eat.");
}
public void sleep()
{
System.out.println("I can sleep.");
}
}
//Fish 子类
package d922A;
public class Fish extends Animal {
Fish()
{
setKind("鱼");
}
public void eat()
{
System.out.println("吃水草。");
}
public void sleep()
{
System.out.println("睁着眼睡觉。");
}
}
//Dog类
package d922A;
public class Dog extends Animal {
Dog()
{
setKind("狗");
}
public void eat()
{
System.out.println("吃肉。");
}
public void sleep()
{
System.out.println("闭着眼睡觉。");
}
}
//主类
package d922A;
public class E {
public static void main(String[] args) {
Animal a=new Animal();
a.eat();
a.sleep();
Fish f=new Fish();
f.getKind();
f.eat();
f.sleep();
Dog d=new Dog();
d.getKind();
d.eat();
d.sleep();
}
}
//运行结果
I can eat.
I can sleep.
鱼
吃水草。
睁着眼睡觉。
狗
吃肉。
闭着眼睡觉。