创建一个工人类(worker),以及一个list集合,在集合中添加三个工人信息: 姓名 年龄 工资 张三 30 3000 Worker w1=new Worker(“张三”,30,3000); 李四 25 3500 王五 27 3200 用java代码显示
时间: 2023-12-14 07:38:41 浏览: 113
java代码-使用java解决公司成员添加及计算工资模式的源代码
下面是创建工人类(worker)和添加到list集合的Java代码:
```java
class Worker {
private String name;
private int age;
private double salary;
public Worker(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public String toString() {
return "姓名:" + name + " 年龄:" + age + " 工资:" + salary;
}
}
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Worker> workers = new ArrayList<>();
Worker w1 = new Worker("张三", 30, 3000);
Worker w2 = new Worker("李四", 25, 3500);
Worker w3 = new Worker("王五", 27, 3200);
workers.add(w1);
workers.add(w2);
workers.add(w3);
for (Worker worker : workers) {
System.out.println(worker);
}
}
}
```
输出结果如下:
```
姓名:张三 年龄:30 工资:3000.0
姓名:李四 年龄:25 工资:3500.0
姓名:王五 年龄:27 工资:3200.0
```
阅读全文