用java编辑:定义一个表示员工信息的类Employee,Employee类中包含表示姓名、工资、部门的私有成员变量,定义Employee的构造方法实现对表示姓名、工资这两个私有成员变量的初始化后,然后重载Employee的构造方法实现对表示姓名、工资、部门这三个个私有成员变量的初始化后,并根据员工对象创建时所调用构造方法的不同,输出相应的员工信息。例如创建姓名为” WangYi”,工资为8000的员工对象,创建姓名为” WangEr”,工资为7000,部门为"coffe shop"的员工对象,程序的输出信息为: WangYi's salary is 8000. WangEr is work in coffe shop, his salary is 7000. 输出内容如下: WangYi's salary is 8000. WangEr is work in coffe shop, his salary is 7000.
时间: 2023-06-14 16:04:00 浏览: 910
java代码-定义一个员工信息类Employee
5星 · 资源好评率100%
代码如下:
```
public class Employee {
private String name;
private double salary;
private String department;
// 构造方法,初始化姓名和工资
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
// 重载构造方法,初始化姓名、工资和部门
public Employee(String name, double salary, String department) {
this.name = name;
this.salary = salary;
this.department = department;
}
// 输出员工信息
public void printEmployeeInfo() {
if (department == null) {
System.out.println(name + "'s salary is " + salary + ".");
} else {
System.out.println(name + " is work in " + department + ", his salary is " + salary + ".");
}
}
}
```
主函数中可以这样调用:
```
public static void main(String[] args) {
Employee employee1 = new Employee("WangYi", 8000);
Employee employee2 = new Employee("WangEr", 7000, "coffe shop");
employee1.printEmployeeInfo();
employee2.printEmployeeInfo();
}
```
输出结果为:
```
WangYi's salary is 8000.
WangEr is work in coffe shop, his salary is 7000.
```
阅读全文