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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王海平 中级黑马   /  2016-1-22 23:53  /  485 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Demo {
        public static void main(String[] args){
                Cat c=new Cat("黄",5);
                System.out.println(c.getColor()+"\n"+c.getLeg());
                c.catchMouse();
                Dog d=new Dog("绿",3);
                System.out.println(d.getColor()+"\n"+d.getLeg());
                d.lookHome();


        }
}
class Animal {
         String color;
         int leg;
        public Animal(){}
        public Animal(String color,int age){
                        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;
        }
}
class Cat extends Animal {
        public Cat(){}
        public Cat(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 catchMouse(){
                        System.out.println("抓老鼠");
        }

}
class Dog extends Animal {
        public Dog(){}
        public Dog(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 lookHome(){
                        System.out.println("看门");
        }
}

0 个回复

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