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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wubo46 中级黑马   /  2016-5-19 21:42  /  505 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Day_Animal {
        public static void main(String[] args) {
                Cat c = new Cat();
                c.setColor("花");
                c.setLeg(4);
                System.out.println(c.getColor() + "..." + c.getLeg());
                c.eat();
                c.function();
                System.out.println("--------------------------");
                Dog d = new Dog("黄",4);
                System.out.println(d.getColor() + "..." + d.getLeg());
                d.eat();
                d.function();
        }
}
class Animal {
        private String color;
        private int leg;
        //无参
        public Animal (){}
        //有参
        public Animal (String color,int leg){
                this.color = color;
                this.leg = leg;
        }
        //设置set
        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("吃肉");

        }
        public void function(){
                System.out.println("抓老鼠");
        }
}
class Cat extends Animal {
        public Cat (){}
        //隐藏着一个    super();
        public Cat (String color,int leg){

        //如果这是里没有写super();那么自动认为super();指定父类无参。               
        }

        //重写父类的eat方法
        public void eat (){
                System.out.println("吃鱼");
        }
}
class Dog extends Animal{
        public Dog () {}
        //隐藏着一个        super();
        public Dog (String color,int leg) {
                super (color,leg);
        }
        //重写父类的function
        public void function () {
                System.out.println("看门");
        }
}

2 个回复

倒序浏览
看了半天,然而,没看懂
回复 使用道具 举报
一枚小程序员 发表于 2016-5-19 21:50
看了半天,然而,没看懂

注释的不够好
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马