两个小题,救救
//声明一个雇员类,属性:编号,姓名,职位,工资//创建5个雇员类对象,并将其保存在集合中,然后遍历集合并获取每个对象中的元素值
参考代码:
雇员类 Employee.java:
import java.math.BigDecimal;
public class Employee {
private int id;
private String name;
private String post;
private BigDecimal salary;
public Employee() {
}
public Employee(int id, String name, String post, BigDecimal salary) {
this.id = id;
this.name = name;
this.post = post;
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 getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", post='" + post + '\'' +
", salary=" + salary +
'}';
}
}
雇员测试类,EmployeeTest.java :
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class EmployeeTest {
public static void main(String[] args) {
List<Employee> list = new ArrayList<>();
// For 循环创建五个 Employee 实例,保存到 List集合中
for (int i = 1; i <= 5; i++){
list.add(new Employee(i, "eName" + i, "Post" + i, new BigDecimal(1000 * i)));
}
// 因为在类中重写了 toString 方法,所以下面方法可以直接打印 e ,而不用通过一个个 get 来进行打印
for (Employee e : list){
System.out.println("ID: " + e.getId() + "\tName: " + e.getName() + "\tPost: " + e.getPost() + "\tSalary: " + e.getSalary());
}
}
} 这很简单吧,百度一下不就有了 package test;
public class Employee{
private int number;
private String name;
private String post;
private double salary;
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(int number, String name, String post, double salary) {
super();
this.number = number;
this.name = name;
this.post = post;
this.salary = salary;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee(1, "张三1", "保安", 600));
list.add(new Employee(1, "张三2", "保安", 600));
list.add(new Employee(1, "张三3", "保安", 600));
list.add(new Employee(1, "张三4", "保安", 600));
list.add(new Employee(1, "张三5", "保安", 600));
for (Employee e : list) {
System.out.println(e.getName());
} 心驰神往 发表于 2022-4-13 16:17
这很简单吧,百度一下不就有了
确实,属实是急了,回过头发现是能写出来的 qwq 揽一池星河 发表于 2022-4-13 18:44
确实,属实是急了,回过头发现是能写出来的 qwq
你能不能写出来,还是要把贴子结贴。结贴是对答贴者起码的尊重。 ba21 发表于 2022-4-14 10:11
你能不能写出来,还是要把贴子结贴。结贴是对答贴者起码的尊重。
刚回来,结的结的{:5_109:} 面向百度编程就行了
{:10_256:}
页:
[1]