大家帮忙看一下这个程序哪有问题
我写了一个关于继承和抽象的小程序,预想结果是 blackcoat.java中的 name变量能在manager.java中被调用,结果运行结果如下:
大家好!我是盖茨,今年50岁
具有28年的管理经验
对null进行年度工作考核
考核评价:工作认真,继续努力!
考核得分:90
==============================
大家好!我是李阳,今年22岁
工号是201020910205,我具有999的人气指数
我接受了一个任务:设计并完成“我爱购物狂”购物网站,一定保质保量完成!
显示null的地方就是blackcoat.java中的 name应该在的位置
代码:
blackcoat.java文件中的代码:
public class blackcoat extends person{
private int hp;
public blackcoat(){
}
public blackcoat( int age,String name,String sex,String workid,int hp){
super(age,name,sex,workid);
this.hp=hp;
}
public void work(){
System.out.println("工号是"+this.getWorkid()+",我具有"+this.hp+"的人气指数"+
"\n我接受了一个任务:设计并完成“我爱购物狂”购物网站,一定保质保量完成!");
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
}
manager.java文件中的代码:
public class manager extends person{
private int zili;
private int core;
public manager(){
}
public manager(int age,String name,String sex,String workid,int zili,int core){
super(age,name,sex,workid);
this.zili=zili;
this.core=core;
}
public void kaohe(){
blackcoat bt =new blackcoat();
System.out.println("具有"+this.zili+"年的管理经验"+"\n对"+bt.getName()+"进行年度工作考核"
+"\n考核评价:工作认真,继续努力!"+"\n考核得分:"+this.core);
}
public int getZili() {
return zili;
}
public void setZili(int zili) {
this.zili = zili;
}
public int getCore() {
return core;
}
public void setCore(int core) {
this.core = core;
}
}
person.java文件中的代码
public abstract class person {
public int age;
public String name;
public String sex;
public String workid;
public person(){
}
public person( int age,String name,String sex,String workid){
this.age=age;
this.name=name;
this.sex=sex;
this.workid=workid;
}
public void hello(){
System.out.println("大家好!我是"+this.getName()+",今年"+this.getAge()+"岁");
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getWorkid() {
return workid;
}
public void setWorkid(String workid) {
this.workid = workid;
}
}
test.java文件中的代码
public class test {
public static void main(String[]args){
manager mr= new manager(50,"盖茨","男","001",28,90);
mr.hello();
mr.kaohe();
System.out.println("==============================");
blackcoat bt=new blackcoat(22,"李阳","男","201020910205",999);
bt.hello();
bt.work();
}
}
------解决方案--------------------你用了个blackcoat bt =new blackcoat();
bt.getName()不是就个空的吗?
------解决方案--------------------
Java code
public class test {
public static void main(String[]args){
/**
* 我改动了你的这个方法,
* 把bt提前声明好,然后作为一个参数,
*/
manager mr= new manager(50,"盖茨","男","001",28,90);
blackcoat bt=new blackcoat(22,"李阳","男","201020910205",999);