用java写一个employee类
时间: 2024-05-02 11:22:22 浏览: 12
好的,我可以帮你用Java写一个employee类。以下是示例代码:
```
public class Employee {
private String name;
private int age;
private String position;
private double salary;
public Employee(String name, int age, String position, double salary) {
this.name = name;
this.age = age;
this.position = position;
this.salary = salary;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setPosition(String position) {
this.position = position;
}
public String getPosition() {
return position;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary() {
return salary;
}
public static void main(String[] args) {
Employee employee1 = new Employee("John", 30, "Manager", 50000.0);
System.out.println("Name: " + employee1.getName());
System.out.println("Age: " + employee1.getAge());
System.out.println("Position: " + employee1.getPosition());
System.out.println("Salary: $" + employee1.getSalary());
}
}
```
在上面的示例代码中,我们定义了一个名为“Employee”的类,它具有四个属性:姓名、年龄、职位和工资。我们还定义了一个构造函数,可以使用它来创建新的Employee对象。为每个属性定义了相应的setter和getter方法,以便在必要时可以对它们进行更改和检索。最后,我们使用“main”方法来创建一个新的Employee对象,并打印其各个属性的值。
希望这可以回答您的问题。