本帖最后由 forward 于 2013-8-26 21:40 编辑
public class AbstractDeno2
{
public static void main(String[] args)
{
Manager m = new Manager("Ansen","S13006540",8900.0 , 10000.0 );
m.working();
System.out.println(m.getName()+" "+m.getWorkid()+" "+m.getSalary()+" "+m.getBonus());
}
}
/*
假如在开发一个系统时需要对员工进行建模,员工有3个属性:
姓名、工号以及工资,经理也是员工,除了含有员工的属性外。
还有一个奖金属性,请使用继承的思想设计出员工类和经理类,
要求类中提供必要的方法进行属性访问。
*/
abstract class Employee
{
private String name;
private String workid;
private double salary;
public abstract void working();
public Employee(String name , String workid,double salary)
{
this.name = name ;
this.workid =workid;
this.salary = salary;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getWorkid()
{
return workid;
}
public void setWorkid(String workid)
{
this.workid = workid;
}
public double getSalary()
{
return salary;
}
public void setSalary(double salary)
{
this.salary = salary;
}
}
class General extends Employee
{
public General(String name , String workid, double salary)
{
super(name , workid, salary);
}
public void working()
{
System.out.println("我是个普通的工人,我正在车间工作!");
}
}
class Manager extends Employee
{
private double bonus;
public Manager(String name , String workid, double salary, double bonus)
{
super(name , workid, salary);
this.bonus = bonus;
}
public void working()
{
System.out.println("我是公司经理,正在和各位主管们开会!");
}
public double getBonus()
{
return bonus;
}
public void setBonus(double bonus)
{
this.bonus = bonus;
}
}
子类继承父类时候,能不能继承父类的私有属性和方法?可是为什么上面的代码能执行成功呢?
|