编写一个Java程序片段,定义表示雇员的类Employee。雇员的属性包括雇员号、姓名、性别、部门、职位。
方法包括设置雇员号、设置姓名、设置性别、设置部门、、设置职位以及获取雇员号、获取姓名、获取性别、获取部门、获取职位。
5.为习题4中的类Employee增加一个方法public String toString(),该方法把Employee类的对象的所有属性信,
组合成一个字符串以便输出显示,并编写一个Java Application程序,要求创建Employee类的对象,并验证新增加的功能。
法一:
class Employee {
private int EID;
private String name,sex,apartment,post;
public void setEID(int EID){
this.EID=EID;
}
public void setName(String name){
this.name=name;
}
public void setSex(String sex){
this.sex=sex;
}
public void setApartment(String apartment){
this.apartment=apartment;
}
public void setPost(String post){
this.post=post;
}
public int GetEID(){
return EID;
}
public String GetName(){
return name;
}
public String GetSex(){
return sex;
}
public String GetApartment(){
return apartment;
}
public String GetPost(){
return post;
}
public String toString(){
return "雇员号:"+GetEID()+"\n 姓名:"+GetName()+"\n 性别:"+GetSex()+"\n 部门:"+GetApartment()+"\n 职位:"+GetPost();
}
public static void main(String args[]){
Employee E=new Employee();
E.setEID(181800);
E.setName("lina");
E.setSex("女");
E.setApartment("software");
E.setPost("program enignner");
System.out.println(E.toString());
}
}
```java
法二:
public class Employee {
private int GH;
private String Name;
private String Sex;
private String ZW;
private String BM;
public String GetName(String name) {
Name=name;
return this.Name;
}
public void GetGH(int gh){
this.GH=gh;
System.out.println("雇员号:"+this.GH);
}
public void GetSex(String sex) {
this.Sex=sex;
}
public void GetZW(String zw) {
this.ZW=zw;
}
public void GetBM(String bm) {
this.BM=bm;
}
public static void main(String[] args) {
Employee employee=new Employee();
employee.GetName("黄化");
employee.GetGH(18180049);
employee.GetSex("男");
employee.GetBM("管理部");
employee.GetZW("董事长");
System.out.println("姓名:"+Name);
}
}