•定义两个类,经理类Manager,程序员类Coder
时间: 2024-10-25 18:00:58 浏览: 26
在面向对象编程中,我们可以定义两个类分别代表"经理"和"程序员",这两个类通常会包含它们特有的属性和行为。让我们简要地描述一下:
**Manager类(经理)**
```java
public class Manager {
private String name;
private int id;
private double salary;
private List<Worker> team; // 管理的团队成员
// 构造函数
public Manager(String name, int id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
this.team = new ArrayList<>();
}
// 添加团队成员
public void addTeamMember(Worker member) {
this.team.add(member);
}
// 其他可能的方法,比如查看团队人数、提升员工等
public String getName() { return name; }
// ... getters and setters for id, salary
// 重写toString方法,提供简洁的字符串表示
@Override
public String toString() {
return "Manager{" +
"name='" + name + '\'' +
", id=" + id +
", salary=" + salary +
'}';
}
}
```
**Coder类(程序员)**
```java
public class Coder extends Worker {
private String programmingLanguage;
private List<String> skills;
// 构造函数
public Coder(String name, int id, String programmingLanguage, List<String> skills) {
super(name, id);
this.programmingLanguage = programmingLanguage;
this.skills = skills;
}
// 获取编程语言
public String getProgrammingLanguage() {
return programmingLanguage;
}
// 获取技能列表
public List<String> getSkills() {
return skills;
}
// 重写toString方法,提供个性化的字符串表示
@Override
public String toString() {
return "Coder{" +
"name='" + getName() + '\'' +
", id=" + getId() +
", programmingLanguage='" + programmingLanguage + '\'' +
'}';
}
}
// Worker是经理和程序员共同的父类,可能包含通用属性如姓名和ID
abstract class Worker {
//...
}
```
阅读全文