创建一个员工类,并声明三个职工对象并赋值
时间: 2023-08-04 15:11:35 浏览: 88
以下是一个简单的员工类的示例代码,其中包含姓名、年龄和工资三个属性:
```python
class Employee:
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
# 创建三个职工对象并赋值
employee1 = Employee("张三", 25, 5000)
employee2 = Employee("李四", 30, 8000)
employee3 = Employee("王五", 35, 10000)
```
上述代码中,我们通过 `__init__` 方法初始化了员工对象的属性,并创建了三个具体的职工对象 `employee1`、`employee2` 和 `employee3`,并分别为他们赋予了不同的属性值。
相关问题
创建一个职工类Emplyee,其属性有员工编号id、姓名name、性别sex、薪水 salary;再声明三个职工对象并赋值,然后依次使用List、Set、Map集合来实现对职工对象数据的存储、添加、删除、修改、查询等操作,并输出。
首先,我们创建一个名为`Employee`的简单Java类,代表职工:
```java
public class Employee {
private int id;
private String name;
private String sex;
private double salary;
// 构造函数
public Employee(int id, String name, String sex, double salary) {
this.id = id;
this.name = name;
this.sex = sex;
this.salary = salary;
}
// 获取和设置属性的方法
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
public double getSalary() { return salary; }
public void setSalary(double salary) { this.salary = salary; }
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", salary=" + salary +
'}';
}
}
```
接下来,我们可以创建三个职工对象并演示如何使用不同的集合结构进行存储:
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
// 创建员工对象
Employee emp1 = new Employee(1, "张三", "男", 5000);
Employee emp2 = new Employee(2, "李四", "女", 6000);
Employee emp3 = new Employee(3, "王五", "男", 7000);
// 使用List存储
List<Employee> list = new ArrayList<>();
list.add(emp1);
list.add(emp2);
list.add(emp3);
System.out.println("List: " + list);
// 使用Set(无重复元素)
Set<Employee> set = new HashSet<>(list);
System.out.println("Set (unique): " + set);
// 使用Map存储(key-value形式)
Map<Integer, Employee> map = new HashMap<>();
map.put(emp1.getId(), emp1);
map.put(emp2.getId(), emp2);
map.put(emp3.getId(), emp3);
System.out.println("Map: " + map);
// 删除、修改和查询操作示例
list.remove(emp1); // 删除emp1
emp2.setName("李四新"); // 修改emp2的姓名
System.out.println("After remove and modify:");
System.out.println("List after removing emp1: " + list);
System.out.println("Modified emp2: " + map.get(map.keySet().iterator().next()));
// 查询操作(假设id作为键)
Employee queryEmp = map.get(2); // 查找id为2的员工
if (queryEmp != null) {
System.out.println("Found employee with id 2: " + queryEmp);
} else {
System.out.println("Employee not found.");
}
}
}
```
1、定义Employee员工类: 私有属性:姓名,工号,工资; 公有的构造方法:3个参数,为三个属性赋值; 公有方法: 每个属性的getter和setter方法; print方法,输出员工的所有信息; 2、定义Manager为Employee的子类: 新的私有属性:津贴; 公有构造方法:4个参数,初始化该类的四个属性。 print方法:输出4个属性的值。 3、在Demo类中编写main方法,声明Employee类型的对象p1,为其创建Employee对象,对象的属性值输入;声明Employee类的对象p2,为其创建Manager类对象,属性值输入;分别调用p1和p2的print方法输出各自的信息。
好的,以下是代码实现:
```
// Employee员工类
public class Employee {
private String name;
private String id;
private double salary;
public Employee(String name, String id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void print() {
System.out.println("姓名:" + name + ",工号:" + id + ",工资:" + salary);
}
}
// Manager经理类,继承自Employee员工类
public class Manager extends Employee {
private double allowance;
public Manager(String name, String id, double salary, double allowance) {
super(name, id, salary);
this.allowance = allowance;
}
public double getAllowance() {
return allowance;
}
public void setAllowance(double allowance) {
this.allowance = allowance;
}
@Override
public void print() {
System.out.println("姓名:" + getName() + ",工号:" + getId() + ",工资:" + getSalary() + ",津贴:" + allowance);
}
}
// Demo类
public class Demo {
public static void main(String[] args) {
// 创建Employee对象并输出信息
Employee p1 = new Employee("张三", "001", 5000);
p1.print();
// 创建Manager对象并输出信息
Manager p2 = new Manager("李四", "002", 8000, 2000);
p2.print();
}
}
```
运行结果:
```
姓名:张三,工号:001,工资:5000.0
姓名:李四,工号:002,工资:8000.0,津贴:2000.0
```
阅读全文