class Test_Exetends {
public static void main(String[] args) {
Manager m = new Manager();
m.setName("经理是笨蛋");
m.setId(741);
m.setSalary(50000.0);
m.setMoney(3.0);
m.work();
System.out.println(m.getName() + "----" + m.getId() + "------" +m.getSalary() + "-------" + m.getMoney());
Programmer p = new Programmer();
p.setName("棒棒的");
p.setId(666);
p.setSalary(20000.0);
p.work();
System.out.println(p.getName() + "----" + p.getId() + "------" +p.getSalary());
}
}
/*
需求一:
项目经理类 manager
属性:姓名 工号 工资 奖金money
行为:工作work
程序员类programmer
属性:姓名 工号 工资
行为:工作work
*/
class Person {
private String name; //封装成员变量
private int id;
private double salary;
public Person () {} //空参构造
//有参构造
public Person (String name,int id ,double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
//设置,获取属性
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void work() {
System.out.println("好好工作!");
}
}
//定义一个Manager子类,继承Person
class Manager extends Person {
private double money;
public void setMoney(double money) {
this.money = money;
}
public double getMoney() {
return money;
}
public Manager(){}//空参构造,作用:创建Manager的空参对象
//有参构造 作用创建Manager的有参对象
public Manager(String name,int id,double salary,double money){
super(name,id,salary);
}
}
class Programmer extends Person {
//空参构造 作用:创建Programmer的无参对象
public Programmer() {}
// 有参构造 作用: 创建Programmer的有参对象
public Programmer(String name,int id , double salary) {
super(name,id,salary);
}
} |
|