创建动物父类跟狗猫子类
abstract class Animal1{ //抽象类
String name;
int age;
String variety;
public Animal1(){
}
public Animal1(String name,int age,String variety){ //有参构造
this.age=age;
this.name=name;
this.variety=variety;
}
abstract public void sheep(); //抽象方法
}
class Dog3 extends Animal1{ //继承抽象类
public Dog3(){
}
public Dog3(String name, int age, String variety){ //有参构造
super(name,age,variety);
}
public void sheep(){ //必须重写抽象方法
System.out.println("狗侧着睡");
}
}
class Cat3 extends Animal1{ //继承抽象类
public Cat3(){
}
public Cat3(String name, int age, String variety){
super(name,age,variety);
}
public void sheep(){ //必须重写抽象方法
System.out.println("猫躺着睡");
}
}
public class AbstractTest {
public static void main(String[] args) {
Animal1 d1=new Dog3("花花",3,"中华田园犬"); //抽象多态
d1.sheep();
Animal1 c1=new Cat3("球球",1,"中华田园猫"); //抽象多态
c1.sheep();
}
}
标签:java,String,Animal1,variety,abstract,关键字,age,public,name
From: https://www.cnblogs.com/ndmtzwdx/p/18431567