首先定义一个雇员类EMP,类的属性包括雇员编号ID,雇员名Name,年龄Age,性别Sex和专业Major,成员方法包括构造方法、属性的get方法和set方法,打印雇员信息的方法PrintEMP,然后在主类的main()函数中为5号计算机网络专业的23岁男性雇员王强定义一个相应的对象emp,并调用对象的PrintEMP ()方法在屏幕上显示其信息。
代码刚写的还热乎,展开粘到环境里直接跑就行,注意换行
public class ForZhiDao {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EMP emp = new EMP("5", "23", "王强", "男", "计算机网络专业");
PrintEMP(emp);
}
public static void PrintEMP(EMP emp) {
System.out.println(emp.toString());
}
static class EMP {
String ID;
String Name;
String Age;
String Sex;
String Major;
public EMP(String iD, String name, String age, String sex, String major) {
super();
ID = iD;
Name = name;
Age = age;
Sex = sex;
Major = major;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAge() {
return Age;
}
public void setAge(String age) {
Age = age;
}
public String getSex() {
return Sex;
}
public void setSex(String sex) {
Sex = sex;
}
public String getMajor() {
return Major;
}
public void setMajor(String major) {
Major = major;
}
public String toString() {
return "ID:" + ID + ",Name:" + Name + ",Age:" + Age + ",Sex:" + Sex + ",Major:" + Major;
}
}
}