猫.385 2022-07-06 14:56 采纳率: 0%
浏览 599

定义一个员工类employee

定义一个员工类employee,具有属性 id(String),属性姓名name(String)、年龄age(int)和薪水salary(float),函数成员包括:构造函数,设置/读取id函数setId和getId,设置/读取姓名函数setName和getName,设置/读取年龄函数setAge和getAge,设置/读取薪水函数setSalary和 getSalary,输出员工基本信息的函数print。在主函数中首先创建任一员工对象emp1最后输出 emp1 的基本信息。

  • 写回答

2条回答 默认 最新

  • it_hao528 2022-07-06 15:48
    关注

    代码如下:

    public class Employee {
        // 属性
        private int id;
        private String name;
        private int age;
        private float salary;
    
        // 构造函数
        public Employee() {
        }
    
        public Employee(int id, String name, int age, float salary) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.salary = salary;
        }
    
        // getter and setter
        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 int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public float getSalary() {
            return salary;
        }
    
        public void setSalary(float salary) {
            this.salary = salary;
        }
    
        // 输出
        public void print() {
            System.out.println("id = " + id + ", name = " + name + ", age = " + age + ", salary = " + salary);
        }
    
        // main
        public static void main(String[] args) {
            Employee emp1 = new Employee(1, "zhangsan", 18, 2000);
            emp1.print();
        }
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 7月6日