定义一个抽象类Animal,然后再定义两个继承Animal的子类,在测试类中使用所定义的类,观察运行效果
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()
{
}
}
子类:
public class Fish extends Animal{
public void eat()
{
System.out.println(getType()+" 吃");
}
public void sleep()
{
System.out.println(getType()+" 睡");
}
}
public class Dog extends Animal{
public void eat()
{
System.out.println(getType()+" 吃");
}
public void sleep()
{
System.out.println(getType()+" 睡");
}
}
测试类
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();
}
}
你这个问的是个啥问题