在你的程序中,向输出语句中直接传递了对象名作为参数,那么输出结果将会是变量的地址,而非其所指向的内容
更改后的程序:
public class AbstractDemo {
public static void main(String[] args) {
Pro pro = new Pro("胡平燕", "112", 1000);
Manager manager = new Manager("胡平燕22", "113", 3000, 500);
pro.work();
System.out.println("\t" + pro.getName()+" "+pro.getId()+" "+
pro.getPay());
manager.work();
System.out.println("\t" + manager.getName()+" "+manager.getId()+" "+
manager.getPay()+" "+manager.getBonus());
}
}
abstract class Employee {
private String name;
private String id;
private double pay;
Employee(String name, String id, double pay) {
this.name = name;
this.id = id;
this.pay = pay;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getPay() {
return pay;
}
public void setPay(double pay) {
this.pay = pay;
}
void work(){}
}
class Pro extends Employee {
Pro(String name, String id, double pay) {
super(name, id, pay);
}
void work() {
System.out.print("普通员工");
}
}
class Manager extends Employee {
private int bonus;
Manager(String name, String id, double pay, int bonus) {
super(name, id, pay);
this.bonus = bonus;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
void work() {
System.out.print("公司经理");
}
}
|
|