使用Java实现编写一个控制台程序,实现类的继承,基类实现员工的基本信息属性,子类实现员工的基本技能,然后在主程序调用基类和子类实现存储和检索该员工的信息等,请通过以下抽象类抽象方法的方式实现该程序。
时间: 2024-05-10 08:15:49 浏览: 61
好的,以下是一个简单的实现示例:
```java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
abstract class Employee {
protected String name;
protected int age;
protected String gender;
protected String department;
public Employee(String name, int age, String gender, String department) {
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
}
public abstract void showSkill();
}
class Programmer extends Employee {
private String language;
public Programmer(String name, int age, String gender, String department, String language) {
super(name, age, gender, department);
this.language = language;
}
public void showSkill() {
System.out.println(name + " is good at programming in " + language);
}
}
class Designer extends Employee {
private String tool;
public Designer(String name, int age, String gender, String department, String tool) {
super(name, age, gender, department);
this.tool = tool;
}
public void showSkill() {
System.out.println(name + " is good at designing with " + tool);
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<String, Employee> employeeMap = new HashMap<>();
while (true) {
System.out.println("1. Add employee");
System.out.println("2. Search employee");
System.out.println("3. List all employees");
System.out.println("4. Exit");
int option = scanner.nextInt();
if (option == 1) {
System.out.println("Enter name:");
String name = scanner.next();
System.out.println("Enter age:");
int age = scanner.nextInt();
System.out.println("Enter gender:");
String gender = scanner.next();
System.out.println("Enter department:");
String department = scanner.next();
System.out.println("Enter skill type (1 for programming, 2 for designing):");
int skillOption = scanner.nextInt();
if (skillOption == 1) {
System.out.println("Enter programming language:");
String language = scanner.next();
Employee employee = new Programmer(name, age, gender, department, language);
employeeMap.put(name, employee);
System.out.println("Employee added successfully.");
} else if (skillOption == 2) {
System.out.println("Enter design tool:");
String tool = scanner.next();
Employee employee = new Designer(name, age, gender, department, tool);
employeeMap.put(name, employee);
System.out.println("Employee added successfully.");
} else {
System.out.println("Invalid option.");
}
} else if (option == 2) {
System.out.println("Enter name:");
String name = scanner.next();
Employee employee = employeeMap.get(name);
if (employee != null) {
System.out.println("Name: " + employee.name);
System.out.println("Age: " + employee.age);
System.out.println("Gender: " + employee.gender);
System.out.println("Department: " + employee.department);
employee.showSkill();
} else {
System.out.println("Employee not found.");
}
} else if (option == 3) {
for (Employee employee : employeeMap.values()) {
System.out.println("Name: " + employee.name);
System.out.println("Age: " + employee.age);
System.out.println("Gender: " + employee.gender);
System.out.println("Department: " + employee.department);
employee.showSkill();
System.out.println();
}
} else if (option == 4) {
break;
} else {
System.out.println("Invalid option.");
}
}
}
}
```
在这个程序中,我们定义了一个抽象类 `Employee`,它有四个基本信息属性:`name`, `age`, `gender`, `department`,并且它还有一个抽象方法 `showSkill()`,用来展示员工的基本技能。
然后我们定义了两个子类 `Programmer` 和 `Designer`,它们都继承自 `Employee` 类,并且分别实现了 `showSkill()` 方法。
在 `Main` 类中,我们使用了一个 `Map` 来存储所有员工的信息。对于每个员工,我们都可以选择是一个程序员还是一个设计师,并且根据选择来创建不同的子类实例。我们可以通过员工的名字来检索对应的信息,并且可以列出所有员工的信息。
这个程序只是一个简单的示例,你可以根据需要进行修改和扩展。
阅读全文