类的封装实例2

package com.office.apartment;

import com.officee.work.*;
import com.office.staff.*;
import com.office.test.*;
/**
 * 部门类
 * @author Administrator
 *
 */
public class Apartment {
	//成员属性  
	//部门编号、部门名称、员工数组、统计变量
	private String a_id;
	private String a_name;
	private Staff[] staff=new Staff[200];
	private int a_num;//统计部门中员工个数
	
	
	//无参构造方法
	public Apartment() {
		
	}
	
	//部分参数构造方法
	public Apartment(String a_id,String a_name) {
		this.setA_id(a_id);
		this.setA_name(a_name);
	}
	
	//全部带参构造方法
	public Apartment(String a_id,String a_name,Staff[] staff) {
		this.setA_id(a_id);
		this.setA_name(a_name);
		this.setStaff(staff);
	}

	//设置setter和getter方法
	public String getA_id() {
		return a_id;
	}

	public void setA_id(String a_id) {
		this.a_id = a_id;
	}

	public String getA_name() {
		return a_name;
	}

	public void setA_name(String a_name) {
		this.a_name = a_name;
	}

	public Staff[] getStaff() {
		return staff;
	}

	public void setStaff(Staff[] staff) {
		this.staff = staff;
	}

	public int getA_num() {
		return a_num;
	}

	public void setA_num(int a_num) {
		this.a_num = a_num;
	}

	//部门介绍
	public String a_show() {
		String str="部门编号:"+this.getA_id()+"\n"+"部门名称:"+this.getA_name();
		return str;
	}

	//1、将职员信息存入到数组中;2、统计个数
	public void count(Staff staff) {
		for(int i=0;i<this.getStaff().length;i++) {
			if(this.getStaff()[i]==null) {
				this.getStaff()[i]=staff;
				this.a_num++;
				break;
				}
			}		
		}
		
	}

package com.office.staff;

import com.officee.work.*;
import com.office.apartment.*;
import com.office.test.*;

/**
 * 职员类
 * @author Administrator
 *
 */
public class Staff {
	//成员属性
	//员工姓名、工号、年龄、性别、所属部门、职务信息
	private String s_name;
	private String s_id;
	private int s_age;
	private String s_sex;
	private Work work;
	private Apartment apart;

	//无参构造函数
	public Staff() {
		
	}
	
	//全部带参构造函数
	public Staff(String s_name,String s_id,int s_age,String s_sex,Apartment apart,Work work) {
		this.setS_name(s_name);
		this.setS_id(s_id);
		this.setS_age(s_age);
		this.setS_sex(s_sex);
		this.setApart(apart);
		this.setWork(work);
				
			}
	
	//全部带参构造函数
		public Staff(String s_name,String s_id,int s_age,String s_sex) {
			this.setS_name(s_name);
			this.setS_id(s_id);
			this.setS_age(s_age);
			this.setS_sex(s_sex);
		}

	//设置setter和getter方法
	public String getS_name() {
		return s_name;
	}

	public void setS_name(String s_name) {
		this.s_name = s_name;
	}

	public String getS_id() {
		return s_id;
	}

	public void setS_id(String s_id) {
		this.s_id = s_id;
	}

	public int getS_age() {
		return s_age;
	}

	public void setS_age(int s_age) {
		if((s_age>=18)&&(s_age<=65)) {
			 this.s_age=s_age;
		 }else {
			 this.s_age=18;
		 }
	}

	public String getS_sex() {
		return s_sex;
	}

	public void setS_sex(String s_sex) {
		if((s_sex=="男")||(s_sex=="女")) {
			this.s_sex = s_sex;
		}else {
			this.s_sex="男";
		}
	}

	public Apartment getApart() {
		return apart;
	}

	public void setApart(Apartment apart) {
		this.apart = apart;
	}
	
	
	  public Work getWok() { 
		  return work;
	}
	  
	  public void setWork(Work work) { 
		  this.work = work; 
	 }
	 
	//自我介绍方法 
		public String s_show2() {
			String str="姓名:"+this.getS_name()+"\n"+"工号:"+this.getS_id()+"\n"+"性别:"+this.getS_sex()+"\n"+"年龄:"+this.getS_age()+"\n"+"所在部门:"+apart.getA_name()+"\n"+"职务:"+work.getW_name();
			return str;
		}
}
package com.officee.work;

import com.office.apartment.*;
import com.office.staff.*;
import com.office.test.*;

/**
 * 职务类
 * @author Administrator
 *
 */
public class Work {
	//成员属性
	//职务编号、职务名称
	private String w_id;
	private String w_name;
	
	//无参构造方法
	public Work() {
		
	}
	
	//带参构造方法
	public Work(String w_id,String w_name) {
		this.setW_id(w_id);
		this.setW_name(w_name);
	}
	
	//设置setter和getter方法
	public String getW_id() {
		return w_id;
	}
	public void setW_id(String w_id) {
		this.w_id = w_id;
	}
	public String getW_name() {
		return w_name;
	}
	public void setW_name(String w_name) {
		this.w_name = w_name;
	}
	
	public String w_show() {
		String str="职务编号:"+this.getW_id()+"\n"+"职务名称:"+this.getW_name();
		return str;
	}
	

}
package com.office.test;

import com.officee.work.*;
import com.office.apartment.*;
import com.office.staff.*;

/**
 * 测试类
 * @author Administrator
 *
 */
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//职务类对象实例化
		Work work1=new Work("P001","经理");
		Work work2=new Work("P002","助理");
		Work work3=new Work("P003","职员");
		//System.out.println(work1.w_show());
	    //System.out.println("===========");
	    
	    //部门类对象实例化
		Apartment apart1=new Apartment("D001","人事部");
		Apartment apart2=new Apartment("D002","市场部");
		//System.out.println(apart1.a_show());
		//System.out.println("===========");
		
		//职工类对象实例化
		Staff one1=new Staff("张铭","S001",29,"男",apart1,work1);
		Staff one2=new Staff("李艾爱","S002",21,"男",apart1,work2);
		Staff one3=new Staff("孙超","S003",29,"男",apart1,work3);
		Staff one4=new Staff("张美美","S004",26,"男",apart2,work1);
		Staff one5=new Staff("蓝迪","S005",37,"男",apart2,work2);
		Staff one6=new Staff("米莉","S006",24,"男",apart2,work3);
		
		System.out.println(one1.s_show2());
		System.out.println("====================");
		System.out.println(one2.s_show2());
		System.out.println("====================");
		System.out.println(one3.s_show2());
		System.out.println("====================");
		System.out.println(one4.s_show2());
		System.out.println("====================");
		System.out.println(one5.s_show2());
		System.out.println("====================");
		System.out.println(one6.s_show2());
		System.out.println("====================");
		
		apart1.count(one1);
		apart1.count(one2);
		apart1.count(one3);
		apart2.count(one4);
		apart2.count(one5);
		apart2.count(one6);
		
		System.out.println("人事部共有"+apart1.getA_num()+"名员工\n"+"市场部共有"+apart2.getA_num()+"名员工");
		System.out.println("====================");
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值