该小型企业的员工主要有 4 类:经理、技术员、销售员和计时工。要求存 储员工的工号、姓名、岗位、月工资等基本信息
时间: 2023-09-29 12:07:34 浏览: 45
您可以使用一个员工类来存储员工的基本信息,使用属性来表示工号、姓名、岗位和月工资,如下所示:
```python
class Employee:
def __init__(self, emp_id, name, position, salary):
self.emp_id = emp_id
self.name = name
self.position = position
self.salary = salary
```
在这个类中,我们使用 `__init__` 方法来初始化员工的基本信息,将工号、姓名、岗位和月工资作为参数传递给该方法。然后将这些数据存储在属性中,方便后续的访问。
你还可以定义一些其他方法来操作员工对象,例如获取员工信息、修改员工信息等。
相关问题
该小型企业的员工主要有 4 类:经理、技术员、销售员和计时工。要求存 储员工的工号、姓名、岗位、月工资等基本信息
可以使用面向对象编程的思想,定义一个员工类,包含工号、姓名、岗位和月工资等属性,然后派生出经理类、技术员类、销售员类和计时工类。具体实现代码如下:
```python
class Employee:
def __init__(self, id, name, position, salary):
self.id = id
self.name = name
self.position = position
self.salary = salary
class Manager(Employee):
def __init__(self, id, name, position, salary, department):
super().__init__(id, name, position, salary)
self.department = department
class Technician(Employee):
def __init__(self, id, name, position, salary, skill_level):
super().__init__(id, name, position, salary)
self.skill_level = skill_level
class Salesman(Employee):
def __init__(self, id, name, position, salary, sales_volume):
super().__init__(id, name, position, salary)
self.sales_volume = sales_volume
class HourlyWorker(Employee):
def __init__(self, id, name, position, salary, hours_worked):
super().__init__(id, name, position, salary)
self.hours_worked = hours_worked
```
以上代码定义了一个员工类和四个派生类,分别表示经理、技术员、销售员和计时工,每个类都继承了员工类的属性,并添加了额外的属性。可以根据具体需求,对这些类进行扩展和修改。
c++设计小型企业的员工主要有 4 类:经理、技术员、销售员和计时工。要求存 储员工的工号、姓名、岗位、月工资等基本信息
可以使用面向对象的思想设计一个Employee类,然后派生出经理、技术员、销售员和计时工等子类。每个子类中可以添加特定的属性和方法。例如,经理可能有一个部门属性和管理下属员工的方法,销售员可能有一个销售额属性和计算提成的方法。
以下是一个简单的示例代码:
```c++
#include <string>
#include <iostream>
using namespace std;
class Employee {
protected:
int id;
string name;
string position;
float salary;
public:
Employee(int _id, string _name, string _position, float _salary) :
id(_id), name(_name), position(_position), salary(_salary) {}
virtual void display() {
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Position: " << position << endl;
cout << "Salary: " << salary << endl;
}
};
class Manager : public Employee {
private:
string department;
public:
Manager(int _id, string _name, string _department, float _salary) :
Employee(_id, _name, "Manager", _salary), department(_department) {}
void manage() {
cout << "Managing department " << department << endl;
}
void display() {
Employee::display();
cout << "Department: " << department << endl;
}
};
class Technician : public Employee {
private:
string skill;
public:
Technician(int _id, string _name, string _skill, float _salary) :
Employee(_id, _name, "Technician", _salary), skill(_skill) {}
void fix() {
cout << "Fixing with " << skill << endl;
}
void display() {
Employee::display();
cout << "Skill: " << skill << endl;
}
};
class Salesman : public Employee {
private:
float sales;
public:
Salesman(int _id, string _name, float _sales, float _salary) :
Employee(_id, _name, "Salesman", _salary), sales(_sales) {}
float getCommission() {
return sales * 0.1; // 假设提成为销售额的 10%
}
void display() {
Employee::display();
cout << "Sales: " << sales << endl;
}
};
class HourlyWorker : public Employee {
private:
float hours;
public:
HourlyWorker(int _id, string _name, float _hours, float _salary) :
Employee(_id, _name, "Hourly Worker", _salary), hours(_hours) {}
float getPay() {
return hours * salary; // 小时工的工资为时薪乘以工作时间
}
void display() {
Employee::display();
cout << "Hours: " << hours << endl;
}
};
int main() {
Employee* e1 = new Manager(1, "John", "Sales", 5000);
Employee* e2 = new Technician(2, "Mary", "Programming", 4000);
Employee* e3 = new Salesman(3, "Tom", 10000, 3000);
Employee* e4 = new HourlyWorker(4, "Alice", 80, 50);
e1->display();
e2->display();
e3->display();
e4->display();
delete e1;
delete e2;
delete e3;
delete e4;
return 0;
}
```
在这个示例中,我们使用了虚函数和基类指针来实现多态。通过基类指针可以方便地调用子类的方法,而且还可以将不同类型的员工对象存储在同一个数组或容器中,实现更加灵活的管理。