黑马程序员技术交流社区

标题: 今天的学的基本的继承代码,和大家共勉! [打印本页]

作者: 秋alin    时间: 2015-12-14 22:44
标题: 今天的学的基本的继承代码,和大家共勉!
class Test5_Animal {
        public static void main(String[] args) {
                Cat c1 = new Cat("花",4);
                System.out.println(c1.getColor() + "..." + c1.getLeg());
                c1.eat();
                c1.catchMouse();

                Dog d1 = new Dog("黑",2);
                System.out.println(d1.getColor() + "..." + d1.getLeg());
                d1.eat();
                d1.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;
        }

        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("看家");
        }
}

马上就用eclipse了,要脱离痛苦的码基础代码的流程了,还有点小激动呢!希望与大家多多交流!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2