| 
 
| class Demo_Animal { public static void main(String[] args)
 {
 Cat c = new Cat("花",4);                        //创建对象
 System.out.println(c.getColor()+"猫"+c.getLeg()+"条腿");
 c.eat();
 c.catchMouse();
 Dog d = new Dog("黑",4);                        //创建对象
 System.out.println(d.getColor()+"狗"+d.getLeg()+"条腿");
 d.eat();
 d.lookHome();
 }
 }
 class Animal {                                                                //创建动物类
 private String color;                                        //动物的毛色属性
 private int leg;                                                //动物的腿的条数属性
 public Animal() {}                                                //空参构造
 
 public Animal(String color,int leg) {        //带参构造
 this.color = color;
 this.leg = leg;
 }
 
 public void setColor(String color) {        //设置毛色
 this.color = color;
 }
 
 public String getColor() {                                //获取毛色
 return color;
 }
 
 public void setLeg(int leg) {                        //设置腿数
 this.leg = leg;
 }
 
 public int getLeg() {                                        //获取腿数
 return leg = leg;
 }
 public void eat() {                                                //吃饭方法
 System.out.println("吃饭");
 }
 }
 
 class Cat extends Animal {                                        //Cat继承Animal类
 Cat() {}                                                                //空参构造
 Cat(String color,int leg) {                                //带参构造
 super(color,leg);
 }
 public void eat() {                                                //重写eat方法
 System.out.println(this.getColor()+"猫"+"吃猫粮");
 }
 
 public void catchMouse() {                                //抓老鼠方法
 System.out.println(this.getColor()+"猫"+"抓老鼠");
 }
 }
 
 class Dog extends Animal {                                        //Dog继承Animal类
 Dog() {}                                                                //空参构造
 Dog(String color,int leg) {                                //带参构造
 super(color,leg);
 }
 public void eat() {                                                //重写eat方法
 System.out.println(this.getColor()+"狗"+"吃狗粮");
 }
 
 public void lookHome() {                                //看家方法
 System.out.println(this.getColor()+"狗"+"看家");
 }
 }
 
 
 | 
 |