猫狗案例分析,实例及测试

猫狗案例分析,实例及测试

class Test05_Animal {
    public static void main(String[] args) {
        Cat c1 = new Cat("小蓝", 4);
        System.out.println(c1.getColor() + "..." + c1.getLeg());
        c1.eat();
        c1.catchMouse();
        System.out.println("-----------------");
        Dog d1 = new Dog("小黄", 2);
        System.out.println(d1.getColor() + "..." + d1.getLeg());
        d1.eat();
        d1.lookHome();
    }
}
/*
* A:猫狗案例分析
* B:案例演示
    * 猫狗案例继承版
    * 属性:毛的颜色,腿的个数
    * 行为:吃饭
    * 猫特有行为:抓老鼠catchMouse
    * 狗特有行为:看家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 this.color;
    }

    public void setLeg(int leg){
        this.leg = leg;
    }

    public int getLeg(){
        return this.leg;
    }

    public void eat(){
        System.out.println("吃饭");
    }
}

class Cat extends Animal{
    public Cat(){

    }

    public Cat(String color, int leg){
        super(color, leg);
    }

    public void eat(){
        System.out.println("猫吃鱼");
    }

    public void catchMouse(){
        System.out.println("抓老鼠");
    }
}

class Dog extends Animal{
    public Dog(){

    }

    public Dog(String color, int leg){
        super(color, leg);
    }

    public void eat(){
        System.out.println("狗吃肉");
    }

    public void lookHome(){
        System.out.println("看家");
    }
}

猫狗案例分析,实例及测试_第1张图片

你可能感兴趣的:(面向对象)