雇员(Employee)示例:
需求:公司中程序员(programmer)有姓名(name),工号(id),薪水(pay),工作内容(work)。
项目经理(Manager)除了有姓名(name),工号(id),薪水(pay),还有奖金(bonus),工作内容(work)。
对给出需求进行数据建模。- class Test {
- public static void main(String[] args){
- programmer p = new programmer("吴佳峻","85747",11573.27);
- p.work();
- Manager m = new Manager("吴佳峻","85747",11573.27,20000);
- m.work();
- }
- }
- abstract class Employee {
- private String name;
- private String id;
- private double pay;
- public Employee(){}
- public Employee(String name,String id,double pay){
- }
- public String getName(){
- return this.name;
- }
- public String getId(){
- return this.id;
- }
- public double getPay(){
- return this.pay;
- }
- public abstract void work();
- }
- class programmer extends Employee {
- public programmer(){}
- public programmer(String name,String id,double pay){
- super(name,id,pay);
- }
- public void work(){
- System.out.println("程序员的名字叫" + getName() +"工号:" + getId() +"薪水是"+ getPay() + "每天就是打代码");
- }
- }
- class Manager extends Employee {
- private int bonus;
- public Manager(){}
- public Manager(String name,String id,double pay,int bonus){
- super(name,id,pay);
- this.bonus = bonus;
- }
- public void work(){
- System.out.println("项目经理的名字叫" + getName() +"工号:" + getId() +"薪水是"+ getPay() +"奖金是"+ bonus + "每天就是打代码");
- }
- }
复制代码 题目如上
为什么我的代码,打印出来都是null呢?我明明赋值了啊 也没有报错 到底哪步错了啊 |