鱼C论坛

 找回密码
 立即注册
查看: 3390|回复: 8

[已解决]两个小题,救救

[复制链接]
发表于 2022-4-13 13:05:12 | 显示全部楼层 |阅读模式
5鱼币
//声明一个雇员类,属性:编号,姓名,职位,工资
//创建5个雇员类对象,并将其保存在集合中,然后遍历集合并获取每个对象中的元素值
最佳答案
2022-4-13 13:05:13

参考代码:

雇员类 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());
        }

    }
}

最佳答案

查看完整内容

参考代码: 雇员类 Employee.java: 雇员测试类,EmployeeTest.java :
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-13 13:05:13 | 显示全部楼层    本楼为最佳答案   

参考代码:

雇员类 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());
        }

    }
}

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
isdkz + 5 + 5 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-13 16:17:22 | 显示全部楼层
这很简单吧,百度一下不就有了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-13 16:42:05 | 显示全部楼层
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());
                }

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
isdkz + 5 + 5 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-13 18:44:55 | 显示全部楼层
心驰神往 发表于 2022-4-13 16:17
这很简单吧,百度一下不就有了

确实,属实是急了,回过头发现是能写出来的 qwq
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-14 10:11:13 | 显示全部楼层
揽一池星河 发表于 2022-4-13 18:44
确实,属实是急了,回过头发现是能写出来的 qwq

你能不能写出来,还是要把贴子结贴。结贴是对答贴者起码的尊重。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-14 20:45:43 | 显示全部楼层
ba21 发表于 2022-4-14 10:11
你能不能写出来,还是要把贴子结贴。结贴是对答贴者起码的尊重。

刚回来,结的结的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-15 16:24:21 | 显示全部楼层
面向百度编程就行了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-15 16:24:53 | 显示全部楼层

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-16 10:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表