A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

* */
public class Demo02 {
public static void main(String[] args) {
  Enployee en = new Enployee("王武",1000,3653.33); //员工父类
  Manager ma = new Manager("赵六", 199, 5988.22, 3000.11); //经理子类.
  
  System.out.println(en.toString());
  System.out.println(ma.toString());
}
}
class Manager extends Enployee
{
private double bonus;
Manager(String name,int id,double pay,double bonus)
{
  super(name,id,pay);  //既然父类有了,我直接用还不行吗?
  this.bonus = bonus;  //父类没有的,那只能自己想办法了...
}
public double getBonus() {
  return bonus;
}
public void setBonus(double bonus) {
  this.bonus = bonus;
}
@Override
public String toString() { //复写Object中toString方法
  return "Manager [bonus=" + bonus + ", Name =" + getName()
    + ", Id =" + getId() + ", Pay =" + getPay() + "]";
}

}
class Enployee  //定义父类 员工类
{
private String name;  //父类中基本的属性.
private int id;
private double pay;
Enployee(String name,int id,double pay) //初始化参数.
{
  this.name = name;
  this.id = id;
  this.pay = pay;
}
public String getName() {  //必要的set,get方法.
  return name;
}
public void setName(String name) {
  this.name = name;
}
public int getId() {
  return id;
}
public void setId(int id) {
  this.id = id;
}
public double getPay() {
  return pay;
}
public void setPay(double pay) {
  this.pay = pay;
}
@Override
public String toString() { //复写的toString方法.
  return "Enployee [name=" + name + ", id=" + id + ", pay=" + pay + "]";
}
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马