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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

/*
学生老师案例分析
教师学生案例继承版
属性:年龄,名字
行为:吃饭
学生特有行为:学习
教师特有行为:工作
*/
class Demo_Extendperson  {
        public static void main(String[] args) {
                Student s=new Student(23,"jeny");
                System.out.println("student's age is:"+s.getAge()+"student's name:"+s.getName());
                s.eat();
                s.study();

                Teacher t=new Teacher(44,"jens");
                System.out.println("teacher's age is:"+t.getAge()+"teahcer's name is:"+t.getName());
                t.eat();
                t.work();
        }
}
class Person {     //创建person类
        private int age;     //属性年龄
        private String name;    // 属性姓名
        public Person(){} //构造无参方法
        public Person(int age,String name){    //构造有参方法
                this.age=age;
                this.name=name;
        }
        public void setAge(int age){     //设置年龄
                this.age=age;
        }
        public int getAge(){               //获取年龄
                return age;
        }
        public void setName(String name){            //设置名字
                this.name=name;
        }
        public String getName(){               //获取名字
                return name;       
        }
        public void eat(){                 //定义eat的行为
                System.out.println("eat");
        }
}

class Student extends Person {      //构造子类
        public Student(){}   //构造无参方法
        public Student(int age,String name){  //构造有参方法
                super(age,name);    //继承父类的属性
        }
        /*
        public void eat(){   //定义行为
                System.out.println(this.eat());
        }
        */
        public void study(){
                System.out.println("gong gong study,day day up");
        }

}
class Teacher extends Person{            //构造子类
        public Teacher(){}//定义无参方法
        public Teacher(int age,String name){    //定义有参方法
                super(age,name);       //继承父类的属性
        }
        /*public void eat(){    //定义行为
                System.out.println(this.eat());
        }
        */
        public void work(){    //定义行为
                System.out.println("good good work");
        }
}


/*
猫狗的案例
属性:毛的颜色,腿的个数
行为:吃饭
猫特有的行为是:抓老鼠catchMouse
狗特有的行为是:看家lookHome
*/
class Demo_extendcat  {
        public static void main(String[] args) {
                Cat c=new Cat(4,"花色");
                System.out.println("猫的腿的条数是:"+c.getLeg()+"   猫的毛色是:"+c.getColor());
//获取腿的数量  毛色
                c.eat();
                c.catchMouse();
                Dog d=new Dog(4,"黄色");
                System.out.println("狗的腿的条数是:"+d.getLeg()+"   猫的毛色是:"+d.getColor());
                d.eat();
                d.lookHome();
        }
}
class Animal{      //创建一个类
        private String color;     // 属性颜色
        private int leg;        //属性腿的个数
        public Animal(){}     //创建无参方法
        public Animal(int leg,String color){    //创建有参方法
                this.leg=leg;
                this.color=color;
        }

        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(int leg,String color){
                super(leg,color);    //继承父类的属性
        }
        public void eat(){         //行为猫吃鱼
                System.out.println("猫吃鱼");
        }
        public void catchMouse(){
                System.out.println("猫爱抓老鼠");
        }
}
class Dog extends Animal {
        public Dog(){}    //定义无参方法
        public Dog(int leg,String color){    //定义有参方法
                super(leg,color);        //继承父类的属性
        }
        public void eat(){             //定义狗的行为
                System.out.println("狗吃骨头");
        }
        public void lookHome(){
                System.out.println("狗看家");
        }
}

12 个回复

倒序浏览
来来 给顶上
回复 使用道具 举报
wx_KWWw45aw 来自手机 中级黑马 2015-12-8 21:59:41
藤椅
学以致用
回复 使用道具 举报
哦。。。 来自手机 中级黑马 2015-12-8 22:01:07
板凳
顶顶顶。
回复 使用道具 举报
顶顶顶顶顶顶
回复 使用道具 举报

谢谢
回复 使用道具 举报

是的呢!
回复 使用道具 举报
受益匪浅  多谢分享 66666666666666666666666666666666666666
回复 使用道具 举报
顶一个,棒棒哒
回复 使用道具 举报
尼玛大叔 来自手机 中级黑马 2015-12-8 22:12:05
10#
顶起,会不会坚持到明天
回复 使用道具 举报
1126     给黑马币不
回复 使用道具 举报
人丑却保守 发表于 2015-12-8 22:08
受益匪浅  多谢分享 66666666666666666666666666666666666666

66666666666
回复 使用道具 举报
尼玛大叔 发表于 2015-12-8 22:12
顶起,会不会坚持到明天

明天出新内容,济南之声记得关注!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马