编写一个Animal类,具有属性:种类;具有功能:吃、睡。定义其子类Fish
package zhongqiuzuoye;
public class Animal {
//属性
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//功能
public void eat()
{
}
public void sleep()
{
}
}
package zhongqiuzuoye;
public class Fish extends Animal{
public void eat()
{
System.out.println(getType()+" 吃");
}
public void sleep()
{
System.out.println(getType()+" 睡");
}
}
package zhongqiuzuoye;
public class Dog extends Animal{
public void eat()
{
System.out.println(getType()+"吃");
}
public void sleep()
{
System.out.println(getType()+"睡");
}
}
package zhongqiuzuoye;
public class TestAnamal {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Fish f=new Fish();
f.setType("鱼");
f.eat();
f.sleep();
Dog d=new Dog();
d.setType("狗");
d.eat();
d.sleep();
}
}
来源:https://www.cnblogs.com/zs6666/p/5898417.html