A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wx_pmVIrag6 中级黑马   /  2016-7-31 21:38  /  370 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Demo {
        public static void main(String[] args) {
                Cat sc = new Cat();
                sc.setColor("白色");
                sc.setLeg(4);
                sc.show();
                sc.eat();
                sc.run();
                System.out.println();

                Dog st = new Dog("黑色", 4);
                st.show();
                st.eat();
                sc.run();
        }
}
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("动物都是要吃东西的");
        }

        public void run(){
                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 show(){
                System.out.println(getColor() + "猫猫" + getLeg() +"腿");
        }
}

class Dog extends Animal{

        public Dog(){}

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

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

        public void show(){
                System.out.println(getColor() + "狗狗" + getLeg() +"腿");
        }
}

评分

参与人数 1黑马币 +1 收起 理由
r1503882525 + 1 赞一个!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马