继承封装----练习(使用动物猫狗类,同时使用super关键字)

class Animal{
    private String color;
    private int leg;
    Animal(String color, int leg){
        this.color=color;
        this.leg=leg;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public int getLeg() {
        return leg;
    }
    public void setLeg(int leg) {
        this.leg = leg;
    }
    void eat(){
        System.out.println("动物吃东西");
    }

}

class Dog extends Animal{

    Dog(String color, int leg) {
        super(color, leg);
    }
    void eat(){
        System.out.println("狗吃东西");
    }
    void lookHome(){
        System.out.println("狗看家");
    }

}


class Cat extends Animal{
    Cat(String color, int leg){
        super(color, leg);
    }
    void eat(){
        System.out.println("猫吃鱼");
    }
    void catchMouse(){
        System.out.println("猫逮老鼠");
    }
}

public class Test2 {
    public static void main(String[] args) {
        Dog dog = new Dog("黄色",4);
        dog.eat();
        dog.lookHome();
        System.out.println(dog.getColor());
        System.out.println(dog.getLeg());


        Cat cat = new Cat("白色",4);
        dog.eat();
        cat.catchMouse();
        System.out.println(cat.getColor());
        System.out.println(cat.getLeg());
    }

}

你可能感兴趣的:(java基础)