知识点:#继承#及其小案例
1)、在com.soft863.test05.bean包中声明员工类、程序员类、设计师类、架构师类
- 员工类(Employee)属性:编号、姓名、年龄、手机号码
- 程序员类(Programmer)属性:编程语言
- 设计师类(Designer)属性:奖金(bonas)
- 架构师类(Architect)属性:持有股票数量(stock)
要求:属性私有化,无参有参构造,get/set,getInfo方法(考虑重写)
2)、在包中声明测试类Test02
创建各种类的对象,并测试
public class Architect extends Designer{
private int stock;
public Architect(int id, String name, int age, String phoneNo, String language,int bonas,int stock) {
super(id,name,age,phoneNo,language,bonas);
this.stock = stock;
}
public Architect() {
}
@Override
public int getId() {
return id;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
@Override
public String getPhoneNo() {
return phoneNo;
}
@Override
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String getinfo() {
return "Architect{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", phoneNo='" + phoneNo + '\'' +
", stock=" + stock +
'}';
}
}
public class Designer extends Programmer{
private int bonas;
public Designer(int id, String name, int age, String phoneNo, String language,int bonas) {
super(id,name,age,phoneNo,language);
this.bonas = bonas;
}
public Designer() {
}
@Override
public int getId() {
return id;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
@Override
public String getPhoneNo() {
return phoneNo;
}
@Override
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public int getBonas() {
return bonas;
}
public void setBonas(int bonas) {
this.bonas = bonas;
}
@Override
public String getinfo() {
return "Designer{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", phoneNo='" + phoneNo + '\'' +
", bonas=" + bonas +
'}';
}
}
public class Employee {
public int id;
public String name;
public int age;
public String phoneNo;
public Employee() {
}
public Employee(int id, String name, int age, String phoneNo) {
this.id = id;
this.name = name;
this.age = age;
this.phoneNo = phoneNo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public String getinfo() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", phoneNo=" + phoneNo +
'}';
}
}
public class Programmer extends Employee{
private String language;
public Programmer(int id, String name, int age, String phoneNo, String language) {
super(id,name,age,phoneNo);
this.language = language;
}
public Programmer() {
}
@Override
public int getId() {
return id;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
@Override
public String getPhoneNo() {
return phoneNo;
}
@Override
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
@Override
public String getinfo() {
return "Programmer{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", phoneNo='" + phoneNo + '\'' +
", language='" + language + '\'' +
'}';
}
}
text02测试类
public class Test02 {
public static void main(String[] args) {
Employee employee=new Employee(1,"张三",23,"10086");
Programmer programmer=new Programmer(2,"李四",24,"10010","java");
Designer designer=new Designer(3,"王五",25,"114","python",2000);
Architect architect=new Architect(4,"赵六",26,"110","java",3000,100);
System.out.println(employee.getinfo());
System.out.println(programmer.getinfo());
System.out.println(designer.getinfo());
System.out.println(architect.getinfo());
}
}