黑马程序员技术交流社区
标题:
练习题问题
[打印本页]
作者:
wjj410830911
时间:
2013-11-9 14:49
标题:
练习题问题
雇员(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呢?我明明赋值了啊 也没有报错 到底哪步错了啊
作者:
wjj410830911
时间:
2013-11-9 14:56
查了半天也没找到问题 求学长们 帮帮我啊
作者:
Sasson
时间:
2013-11-9 14:58
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){
this.name = name; //这个地方你忘了传值
this.id = id;
this.pay = 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 + "每天就是打代码");
}
}
复制代码
作者:
寻丶丶觅
时间:
2013-11-9 15:00
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(){}
//没写赋值语句,当然返回为null。
public Employee(String name,String id,double pay)
{
this.name = name;
this.id = id;
this.pay = 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 + "每天就是打代码");
}
}
作者:
wjj410830911
时间:
2013-11-9 15:06
啊 谢谢。。。找了半天 原来在这。。。=, =
作者:
wjj410830911
时间:
2013-11-9 15:07
Sasson 发表于 2013-11-9 14:58
啊 谢谢 原来弄了半天问题在这
作者:
花生壳
时间:
2013-11-9 15:35
public Employee(String name, String id, double pay) {
// TODO Auto-generated constructor stub
//你这样写就可以了。没有赋值,看来我的速度还是慢了啊!!!!
this.name=name;
this.id=id;
this.pay=pay;
}
作者:
zdrain
时间:
2013-11-9 16:54
父类带参数构造函数没赋值
public Employee(String name,String id,double pay)
{
this.name = name;
this.id = id;
this.pay = pay;
}
作者:
Mr.__ㄨīn
时间:
2013-11-10 16:47
在17行加上
this.name = name;
this.id = id;
this.pay = pay;
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2