题目描述
- 编写一个程序,用于创建一个名为Employee的父类和两个名为Manager和Director的子类。要求创建Manager和Director的对象,并显示其详细信息。
- Employee类:
- 属性(name,sex,address)
- 方法 show() 显示这些属性
- Manager类:
- 属性(department)
- 重写show方法
- Director类:
- 属性(lengthOfService)
- 重写show方法
核心思想
- extends 关键字的使用
代码实现
- Employee.java
public class Employee {
public String name;
public char sex;
public String address;
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(String name, char sex, String address) {
super();
this.name = name;
this.sex = sex;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void show() {
System.out.println("name=" + name + "sex" + sex + "adress" + address);
}
}
- Manager.java
public class Manager extends Employee {
String department;
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public void show() {
System.out.println(
"name=" + this.name + "sex" + this.sex + "adress" + this.address + "department=" + this.department);
}
}
- Director.java
public class Director extends Employee{
String lengthOfService;
public String getLengthOfService() {
return lengthOfService;
}
public void setLengthOfService(String lengthOfService) {
this.lengthOfService = lengthOfService;
}
@Override
public void show() {
System.out.println(
"name=" + this.name + "sex" + this.sex + "adress" + this.address + "lengthOfService=" + this.lengthOfService);
}
}
本节完!
更多信息交流请加QQ:1406073270