1.创建Animal类
public class Animal {
private String name;
private String age;
public Animal() {
}
public Animal(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
2.创建Cat类
public class Cat extends Animal{
public Cat(){
}
public Cat(String name,String age){
super(name,age);
}
public void 抓老鼠(){
System.out.println("抓老鼠");
}
}
3.创建Dog类
public class Dog extends Animal{
public Dog(){}
public Dog(String name,String age){
super(name,age);
}
public void 看门(){
System.out.println("看门");
}
}
4.测试类 AnimalDemo
public class AnimalDemo {
public static void main(String[] args) {
Cat c1=new Cat();
c1.setName("小白");
c1.setAge("3");
System.out.println(c1.getName()+","+c1.getAge());
c1.抓老鼠();
System.out.println("-----------------------------");
Cat c2=new Cat("草莓","2");
System.out.println(c2.getName()+","+c2.getAge());
c2.抓老鼠();
System.out.println("-----------------------------");
Dog d1=new Dog();
d1.setName("大黄");
d1.setAge("8");
System.out.println(d1.getName()+","+d1.getAge());
d1.看门();
System.out.println("-----------------------------");
Dog d2=new Dog("小黑","7");
System.out.println(d2.getName()+","+d2.getAge());
d2.看门();
}
}
--------------------------------------------------------------------------------