猫和狗(继承)
package cn.tedu_01;
public class Animal {
private String name;
private int age;
public Animal() {
}
public Animal(String name,int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package cn.tedu_01;
public class Cat extends Animal {
public Cat() {
}
public Cat(String name, int age) {
super(name, age);
}
public void catchMouse(){
System.out.println("猫抓老鼠");
}
}
package cn.tedu_01;
public class Dog extends Animal {
public Dog() {
}
public Dog(String name, int age) {
super(name, age);
}
public void lookDoor(){
System.out.println("狗看门");
}
}
package cn.tedu_01;
public class AnimalDemo {
public static void main(String[] args) {
Cat c1=new Cat();
c1.setName("加菲猫");
c1.setAge(5);
System.out.println(c1.getName()+","+c1.getAge());
c1.catchMouse();
Cat c2=new Cat("加菲猫",5);
System.out.println(c2.getName()+","+c2.getAge());
c2.catchMouse();
}
}