class Employee: def __init__(self, name, id, salary): self.__name = name self.__id = id self.__salary = salary def showMessage(self): print("员工信息:") print("姓名:{};id号:{};薪金:{}元;".format(self.__name, self.__id, self.__salary))class SaleManager(Employee): def __init__(self, name, id, salary, salesRevenue, salesProfit): super().__init__(name, id, salary) self.__salesRevenue = salesRevenue self.__salesProfit = salesProfit def showMessage(self): super().showMessage() print("扩展信息:销售收入:{}元,销售利润:{}元。".format(self.__salesRevenue, self.__salesProfit))# 测试代码s = SaleManager("张三", 10001, 10000, 1000000, 100000)print("销售经理信息:")s.showMessage()
时间: 2024-01-28 15:05:13 浏览: 149
这是一段 Python 代码,它展示了一个简单的面向对象编程的例子。其中定义了一个 Employee 类和一个 SaleManager 类,SaleManager 类继承了 Employee 类,并在其基础上添加了两个属性:销售收入和销售利润。同时,SaleManager 类重写了 Employee 类的 showMessage 方法,在显示员工基本信息的同时,还会显示扩展信息。
在测试代码中,我们创建了一个 SaleManager 对象 s,并调用了它的 showMessage 方法,打印出了销售经理的信息,包括基本信息和扩展信息。
相关问题
class Employee: def __init__(self, id, name, title, department, salary): self.id = id self.name = name self.title = title self.department = department self.salary = salaryclass HRSystem: def __init__(self): self.employees = [] def add_employee(self, employee): self.employees.append(employee) def remove_employee(self, id): for i, employee in enumerate(self.employees): if employee.id == id: self.employees.pop(i) return True return False def find_employee(self, id): for employee in self.employees: if employee.id == id: return employee return None def update_employee(self, id, name=None, title=None, department=None, salary=None): employee = self.find_employee(id) if employee: if name: employee.name = name if title: employee.title = title if department: employee.department = department if salary: employee.salary = salary return True return False
这段代码定义了两个类:Employee(员工)和HRSystem(人力资源系统)。Employee类包括了员工的id、姓名、职称、部门和薪资等属性,并且定义了一个构造函数__init__()。HRSystem类包括了员工列表employees,以及添加、删除、查找和更新员工信息等方法。其中,add_employee()方法可以添加新员工,remove_employee()方法可以删除指定id的员工,find_employee()方法可以查找指定id的员工,update_employee()方法可以更新指定id的员工的信息。
需要注意的是,这段代码只是定义了类和方法,还需要在程序中实例化HRSystem类,才能使用这些方法。例如:
```
hr_system = HRSystem()
employee = Employee(1001, 'Tom', 'Manager', 'Sales', 5000.0)
hr_system.add_employee(employee)
```
以上代码实例化了HRSystem类,并创建了一个员工对象employee,并将其添加到HRSystem的员工列表中。
用java语言编写代码class Employees: def __init__(self, name, address, emp_id, salary, years_of_service): self.name = name self.address = address self.emp_id = emp_id self.salary = salary self.years_of_service = years_of_service def change_salary(self, percentage): if percentage <= 0: raise ValueError("The percentage must be positive.") self.salary *= (1 + percentage / 100) class Managers(Employees): def __init__(self, name, address, emp_id, salary, years_of_service, level): super().__init__(name, address, emp_id, salary, years_of_service) self.level = level def change_salary(self, percentage): if percentage <= 0: raise ValueError("The percentage must be positive.") if percentage > 20: raise ValueError("Managers can only get a maximum of 20% raise.") self.salary *= (1 + percentage / 100) employees = [ Employees("Alice", "123 Main Street", 1001, 50000, 3), Managers("Bob", "456 Oak Avenue", 1002, 80000, 5, 2), Employees("Charlie", "789 Pine Boulevard", 1003, 60000, 1) ] for employee in employees: employee.change_salary(10) print(f"{employee.name}: ${employee.salary:.2f}")
这段代码是用 Python 语言编写的,不是 Java。它定义了两个类:Employees 和 Managers,其中 Managers 继承自 Employees。每个类都有一个构造函数和一个 change_salary 方法,用于设置员工的基本信息和调整员工的薪水。在主函数中,创建了一个包含三个员工的列表,然后对每个员工调用 change_salary 方法,将薪水增加 10%,最后输出每个员工的姓名和薪水。
阅读全文