day03--面向对象--abstract关键字

package day04;

/*
 * abstract:抽象的,包含抽象方法的类叫抽象类
 * 
 * 特点:抽象类也是一个类,只不过没有足够的信息来描述某一事物行为的方法
 */
abstract class Animal {

    public abstract void bellow();
}

class Dog extends Animal {// 抽象类也是一个类,可以被继承,并且必须实现父类的抽象方法

    public void bellow() {// 实现了父类中的抽象方法
        System.out.println("汪汪汪...");
    }

}

class Cat extends Animal {

    public void bellow() {
        System.out.println("喵喵喵...");

    }

}

public class Demo2 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        bellow(dog);

        Cat cat = new Cat();
        bellow(cat);

    }

    public static void bellow(Animal animal) {
        animal.bellow();
    }

}
package day04;

/*
 * 需求:公司中程序员有姓名,工号,薪水,工作内容
 * 项目经理除了有姓名,工号,薪水,还有奖金,工作内容.
 * 对给出需求进行数据建模
 */
abstract class Employee {
    private String name;
    private int id;
    private int salary;

    public Employee(String name, int id, int salary) {
        this.name = name;
        this.id = id;
        this.salary = salary;
    }

    abstract public void doWork(String msg);

    public void printMsg() {
        System.out.println("name:" + name + " id:" + id + " salary:" + salary);

    }

}

class Programmer extends Employee {

    public Programmer(String name, int id, int salary) {
        super(name, id, salary);
    }

    public void doWork(String msg) {
        System.out.println("程序员正在敲代码" + msg);

    }

}

class Manager extends Employee {
    public Manager(String name, int id, int salary, int bonus) {
        super(name, id, salary);
        this.bonus = bonus;
    }

    private int bonus;// 奖金

    public void printMsg() {
        super.printMsg();
        System.out.print("bonus:" + bonus);

    }

    public void doWork(String msg) {
        System.out.println("项目经理正在" + msg);
    }

}

public class Demo4 {

    public static void main(String[] args) {
        Programmer p1 = new Programmer("小李", 12, 3000);

        p1.doWork("coding中");
        p1.printMsg();

        Manager m1 = new Manager("小王", 13, 6000, 40000);
        m1.doWork("谈项目中");
        m1.printMsg();

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值