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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 鬼魅_vYpyK 中级黑马   /  2015-12-9 22:46  /  1604 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

案例一、
class  Demo_AbstractEmployee {
        public static void main(String[] args) {
                Coder c=new Coder("小敏","0012",999.9);      //创建程序员对象
                        c.work();      //调用方法
                Manager m=new Manager("小闽","0012",999.9,3457);     //创建项目经理对象
                        m.work();     //调用方法
        }
}
/*
对程序员类进行设计
程序员包括3个属性姓名,员工号,工资
经理除了有与程序猿相同的属性还有一个奖金的属性,姓名,员工号,工资,奖金
请使用
*/
abstract class Employee {    //抽象员工类
        private String name;     //私有属性名字
        private String id;           //私有属性员工号
        private double salary;            //私有属性工资
        public Employee(){}//定义空参方法
        public Employee(String name,String id,double salary){  //定义有参方法
                this.name=name;      
                this.id=id;
                this.salary=salary;
        }
        public void setName(String name){    //设置姓名
                this.name=name;
        }
        public String getName(){       //获取姓名
                return name;
        }
        public void setId(String id){    //设置id 员工号
                this.id=id;
        }
        public String getId(){     // //获取id 员工号
                return id;
        }
        public void setSalary(double salary){    //设置员工工资号
                this.salary=salary;
        }
        public double getSalary(){     // //获取工资
                return salary;
        }
        public abstract void work();     //定义行为工作work
}
class Coder extends Employee {
        public Coder(){}   //空参构造
        public Coder(String name,String id,double salary){    //有参构造
                super(name,id,salary);
        }
        public void work(){    //设置行为方法
                System.out.println("我的姓名是:"+super.getName()+"    我的工号是:"+this.getId()+"   我的工资是:"+this.getSalary()+"     我的目标是好好敲代码");
        }
}

class Manager extends Employee {
        private double bonus;
        public Manager(){}   //空参构造
        public Manager (String name,String id,double salary,double bonus){    //有参构造
                super(name,id,salary);
                //sthis.bonus=bonus;
        }
        public void work(){    //设置行为方法
                System.out.println("我的姓名是:"+this.getName()+"    我的工号是:"+this.getId()+"   我的工资是:"+this.getSalary()+"    我的奖金是:"+this.bonus+"   我的工作内容是项目管理");
        }
        }
案例二、
class Demo_Teacherbstract {
        public static void main(String[] args) {
                BaseTeacher b=new BaseTeacher(32,"mr li");
                b.teach();
        }
}
/*
* 具体事物:基础班老师BaseTeacher,就业班老师
        * 共性:姓名,年龄,讲课。
*/
abstract class Teacher {
        private int age;      //定义属性年龄并私有化
        private String name;     //定义属性姓名并私有化
        public Teacher(){}    //空参构造
        public Teacher(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 abstract void teach();   //构造行为
}

class BaseTeacher extends Teacher {
        public BaseTeacher(){}   //空参构造
        public BaseTeacher(int age,String name){     //有参构造
                super(age,name);     //继承父类的属性       
        }
        public void teach(){
                System.out.println("教授基础班的课程");
        }
}

案例三、
class Demo_Superman  {
        public static void main(String[] args) {
                Person p=new Superman();
                System.out.println(p.name);
                        p.谈生意();
                Superman sm=(Superman)p;  //调用Person p=new Superman()中的new Superman 相当于Superman sm=new Superman();
                        sm.fly();
        }
}

class Person {
        String name="john";
        public void 谈生意(){   //定义人的共有方法“谈生意”
                System.out.println("谈生意");
        }
}
class Superman extends Person {
        String name="superman";
        public void 谈生意(){   //定义Superman的行为方法
                System.out.println("谈几亿元的大生意");
        }
        public void fly(){     //定义Superman独有的行为方法
                System.out.println("内裤外穿飞出去救人");
       
        }
}

案例四、
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();
        }
}

/*
猫狗的案例
属性:毛的颜色,腿的个数
行为:吃饭
猫特有的行为是:抓老鼠catchMouse
狗特有的行为是:看家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("狗看家");
        }

}

5 个回复

倒序浏览
刚觉棒棒哒,一块学习多好
回复 使用道具 举报
发了好多,慢慢看看。。。
回复 使用道具 举报
wx_KWWw45aw 来自手机 中级黑马 2015-12-9 23:24:29
板凳
写的好,加油
回复 使用道具 举报
写的好加油
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马