这样应该没有太大问题吧

这样应该没有太大问题吧

package com.imooc.test;

import com.imooc.part.Duty;
import com.imooc.part.Section;
import com.imooc.part.Staff;

public class CompanyTest {

	public static void main(String[] args) {
		// 测试
		Duty duty = new Duty("职员", "D01");
		Duty duty1 = new Duty("助理", "D02");
		Duty duty2 = new Duty("经理", "D03");
		Section sec = new Section("C01", "人事部");
		Section sec1 = new Section("C02", "市场部");
		
		//员工信息介绍
		Staff sta1 = new Staff("S001", "张铭", "男", 29);
		System.out.println(sta1.intronduction(sec, duty2));
		System.out.println("========================");
		Staff sta2 = new Staff("S002", "李艾爱", "女", 21);
		System.out.println(sta2.intronduction(sec, duty1));
		System.out.println("========================");
		Staff sta3 = new Staff("S004", "孙超", "男", 29);
		System.out.println(sta3.intronduction(sec, duty));
		System.out.println("========================");
		Staff sta4 = new Staff("S005", "张美美", "女", 26);
		System.out.println(sta4.intronduction(sec1, duty));
		System.out.println("========================");
		Staff sta5 = new Staff("S006", "蓝迪", "男", 37);
		System.out.println(sta5.intronduction(sec1, duty2));
		System.out.println("========================");
		Staff sta6 = new Staff("S007", "米莉", "女", 24);
		System.out.println(sta6.intronduction(sec1, duty));
		System.out.println("========================");
		sec.addStaff(sta1);
		sec.addStaff(sta2);
		sec.addStaff(sta3);
		sec1.addStaff(sta4);
		sec1.addStaff(sta5);
		sec1.addStaff(sta6);
		System.out.println(sec.getSectionName()+"共有"+sec.getStaffNum()+"名员工");
		System.out.println(sec1.getSectionName()+"共有"+sec1.getStaffNum()+"名员工");
		
		

	}

}


package com.imooc.part;

/**
 * 部门类
 * @author Zhan
 *
 */

public class Section {
	
	//成员属性:部门编号、部门名称、部门员工信息、部门员工个数
	private String sectionNo;
	private String sectionName;
	private Staff[] myStaffs;
	private int staffNum;
	
	//无参构造
	public Section(){
		
	}
	
	//带参构造,实现对部门编号、部门名称的赋值
	public Section(String sectionNo,String sectionName){
		this.setSectionNo(sectionNo);
		this.setSectionName(sectionName);
	}
	
	//sectionNo的getter方法和setter方法
	public void setSectionNo(String sectionNo){
		this.sectionNo = sectionNo;
	}
	
	public String getSectionNo(){
		return sectionNo;
	}
	
	//sectionName的setter和getter方法
	public void setSectionName(String sectionName){
		this.sectionName = sectionName;
	}
	
	public String getSectionName(){
		return sectionName;
	}
	
	//myStaffs的setter和getter方法
	public void setMyStaffs(Staff[] mysStaffs){
		this.myStaffs = myStaffs;
	}
	
	/**
	 * 获取该部门的员工信息 如果保存员工信息的数组未被初始化,则先初始化长度200
	 * @return
	 */
	public Staff[] getMyStaffs(){
		if(this.myStaffs==null){
			this.myStaffs = new Staff[200];
		}
		return myStaffs;
		
	}
	
	//staffNum的setter和getter方法
	public void setStaffNum(int staffNum){
		this.staffNum = staffNum;
	}
	
	public int getStaffNum(){
		return staffNum;
	}
	
	/**
	 * 部门介绍的方法
	 * @return 部门介绍的相关信息,包括部门名称、部门编号
	 */
	public String info(){
		String str = "部门信息如下:\n部门名称:"+this.getSectionName() + "\n部门编号:" + this.getSectionNo();
		return str;
	}
	
	public void addStaff(Staff sta){
		/**
		 * 1、将员工保存到数组中
		 * 2、将员工个数保存到staffNum
		 */
		for(int i=0;i<this.getMyStaffs().length;i++){
			if(this.getMyStaffs()[i]==null){
				sta.setStaffSection(this);
				this.getMyStaffs()[i]=sta;
				this.staffNum = i+1;
				return;
			}
		}
	}
	
}


package com.imooc.part;

/**
 * 职务类
 * @author Zhan
 *
 */
public class Duty {
	//成员属性:职务编号、职务名称
	private String dutyName;
	private String dutyNo;
	
	//无参构造方法
	public Duty(){
		
	}
	
	//带参构造对全部属性赋值
	public Duty(String dutyName,String dutyNo){
		this.setDutyName(dutyName);
		this.setDutyNo(dutyNo);
	}
	
	//dutyName的getter和setter方法
	public String getDutyName() {
		return dutyName;
	}
	public void setDutyName(String dutyName) {
		this.dutyName = dutyName;
	}
	
	//dutyNo的getter和setter方法
	public String getDutyNo() {
		return dutyNo;
	}
	public void setDutyNo(String dutyNo) {
		this.dutyNo = dutyNo;
	}
	
}


package com.imooc.part;

/**
 * 员工类
 * 
 * @author Zhan
 * 
 */

public class Staff {
	// 成员属性:姓名、工号、年龄、性别、所属部门、职务信息
	private String staffNo;
	private String staffName;
	private String staffSex;
	private int staffAge;
	private Section staffSection;
	private String staffDuty;

	// 无参构造方法
	public Staff() {

	}

	// 多参构造方法,实现对姓名,工号,年龄,性别,赋值
	public Staff(String staffNo, String staffName, String staffSex,
			int staffAge) {
		this.setStaffNo(staffNo);
		this.setStaffName(staffName);
		this.setStaffSex(staffSex);
		this.setStaffAge(staffAge);
	}

	// staffNo的setter和getter方法
	public String getStaffNo() {
		return staffNo;
	}

	public void setStaffNo(String staffNo) {
		this.staffNo = staffNo;
	}

	// staffName的setter和getter方法
	public String getStaffName() {
		return staffName;
	}

	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	// staffSex的setter和getter方法
	public String getStaffSex() {
		return staffSex;
	}

	/**
	 * 给性别赋值,限制性别只能是“男”或者“女”,否则强制赋值为“男”
	 * 
	 * @param staffSex
	 *            传入的性别
	 */
	public void setStaffSex(String staffSex) {
		if (staffSex != "男" && staffSex != "女") {
			this.staffSex = "男";
		} else {
			this.staffSex = staffSex;
		}
	}

	// staffAge的setter和getter方法
	public int getStaffAge() {
		return staffAge;
	}

	/**
	 * 给年龄赋值,限定必须在10--100之间,反之赋值为18
	 * 
	 * @param staffAge
	 *            传入的年龄
	 */
	public void setStaffAge(int staffAge) {
		if (staffAge < 10 || staffAge > 100) {
			this.staffAge = 30;
		} else {
			this.staffAge = staffAge;
		}
	}

	// staffSection的setter和getter方法
	/**
	 * 获取部门对象,如果没有实例化,先实例化后再返回
	 * 
	 * @return 部门对象信息
	 */
	public Section getStaffSection() {
		if (this.staffSection == null) {
			this.staffSection = new Section();
		}
		return staffSection;
	}

	public void setStaffSection(Section staffSection) {
		this.staffSection = staffSection;
	}

	// staffDuty的setter和getter方法
	public String getStaffDuty() {
		return staffDuty;
	}

	public void setStaffDuty(String staffDuty) {
		this.staffDuty = staffDuty;
	}

	public String intronduction(Section mySection,Duty myDuty) {
		String str = "姓名:" + this.getStaffName() + "\n工号:"
				+ this.getStaffNo() + "\n性别:" + this.getStaffSex() + "\n年龄:"
				+ this.getStaffAge() + "\n所属部门职位:" + mySection.getSectionName()
				+ myDuty.getDutyName();
		return str;
	}

}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

有问题,像你的写法员工与职位之间是没有关联的。像 System.out.println(sta1.intronduction(sec, duty2));这不叫关联,这只是输出一下你传递的参数做一下内容拼接而已。你要用setStaffDuty()为员工设置职位。intronduction()时不要传递参数,用this. getStaffDuty()就好了。所属部门也是同样道理。另:与部门属性一样,职位属性要用职位类对像,而不是用String。如果解决了你的疑惑,请采纳,祝学习愉快~

  • 拉斐爾 提问者 #1
    好的,我在修改看看,感谢
    2018-03-13 17:09:10
  • 拉斐爾 提问者 #2
    还有想请问一下的是不是可以理解为封装的特点就是不同的类之间的关联性
    2018-03-13 17:30:43
  • 好帮手慕雪 回复 提问者 拉斐爾 #3
    上述提到的两个类对象之间的关联与你说的封装不是一回事。
    2018-03-13 17:42:40
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师