黑马程序员技术交流社区
标题:
可以运行编译但有问题
[打印本页]
作者:
walkk
时间:
2016-7-31 01:13
标题:
可以运行编译但有问题
class Demo{
public static void main(String[] args) {
Manager M=new Manager("张三","23",4444,5555);
M.work();
Programmer P=new Programmer("李四","24",9999);
P.work();
}
}
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.salary = salary;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary() {
return salary;
}
public abstract void work();
}
class Manager extends Employee{
private int bonus ;
public Manager(){}
public Manager(String name, String id, double salary,int bonus){
super(name,id,salary);
this.bonus=bonus;
}
public void work() {
System.out.println("我是项目经理:" + getName() + " 我的工号是:" + getId() + ",我的工资是:" + getSalary()+",我的奖金是:"+ bonus);
}
}
class Programmer extends Employee{
public Programmer(){}
public Programmer(String name, String id, double salary){
super(name,id,salary);
}
public void work() {
System.out.println("我是程序员:" + getName() + " 我的工号是:" + getId() + ",我的工资是:" + getSalary());
}
}
我是项目经理:张三 我的工号是:null,我的工资是:4444.0,我的奖金是:5555
我是程序员:李四 我的工号是:null,我的工资是:9999.0
工号为什么是null
作者:
武汉隆美尔
时间:
2016-7-31 11:08
有参构造里少了this.id= id;吧
作者:
walkk
时间:
2016-7-31 13:13
武汉隆美尔 发表于 2016-7-31 11:08
有参构造里少了this.id= id;吧
就是这样.谢了
作者:
qishibutaihuai
时间:
2016-7-31 15:24
this代表当前对象的引用,不加this邻近
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2