c#创建一个员工类,包含姓名工号部门薪资,可以打扫卫生买咖啡 调出一个员工,工号001,部门人力资源部,薪资3000,让张飞先打扫工位,然后去给老板买咖啡
时间: 2024-12-21 21:27:15 浏览: 3
在C#中,我们可以创建一个名为`Employee`的类,包含姓名、工号、部门、薪资等属性,并定义两个方法`SweepDesk()`和`BuyCoffeeForBoss()`来表示员工的工作职责。以下是这个类的基本实现:
```csharp
public class Employee
{
public string Name { get; set; }
public int Id { get; set; }
public string Department { get; set; }
public decimal Salary { get; set; }
// 定义员工的工作方法
public void SweepDesk() => Console.WriteLine($"{Name} is sweeping the desk.");
public void BuyCoffeeForBoss() => Console.WriteLine($"{Name} is buying coffee for the boss.");
// 构造函数用于初始化员工信息
public Employee(string name, int id, string department, decimal salary)
{
Name = name;
Id = id;
Department = department;
Salary = salary;
}
}
// 创建一个具体的员工实例
var zhangFei = new Employee("张飞", 001, "人力资源部", 3000);
// 调用员工方法
zhangFei.SweepDesk();
zhangFei.BuyCoffeeForBoss();
```
当你运行这段代码,会看到如下输出:
```
张飞 is sweeping the desk.
张飞 is buying coffee for the boss.
```
阅读全文