Java

제네릭(Generic)

에어팟맥스 2022. 6. 30. 21:16

 

=== 타입에 제한이 없는 제네릭 클래스 ===

// 클래스 이름 다음에 제네릭 타입 T extends Employee 를 선언한다.
 // 제네릭 타입 T extends Employee 의 뜻은 Box_Employee 클래스에서 선언되어지는 필드의 타입과 
// 메소드의 파라미터 및 리턴타입에서 어떤 클래스라도 타입으로 들어올 수 있다는 것이다. 

 

목표: 임직원, 평직원으로 구분되는 직원들의 직원정보를 표시하려 한다. 직원정보는 1. 아이디, 2. 비밀번호, 3. 성명, 4. 직급 으로 1번부터 4번까지는 동일하지만 5번 항목에서는 임직원은 법인카드번호, 평직원은 연봉으로 다르게 구성하고자 한다.

클래스 목록

 - Main

 - Employee

 - Box_Employee

 - Child_Plain

 - Child_Executive

 

=== Employee 클래스 ===

public class Employee {

	String userid;  // 아이디
    String passwd;  // 암호
    String name;    // 사원명
    String jik;     // 직급 
   
    public Employee() {}
   
    public Employee(String userid, String passwd, String name, String jik) {
       this.userid = userid;
       this.passwd = passwd;
       this.name = name;
       this.jik = jik;
    }
   
    @Override
    public String toString() { // 객체명 치면 리턴이 튀어나옴
      
       return "1.아이디: "+userid+"\n"+
             "2.비밀번호: "+passwd+"\n"+
             "3.성명: "+name+"\n"+
             "4.직급: "+jik+"\n";
   }

 

=== Box_Employee ===

public class Box_Employee<T extends Employee> {

	// field
	private List<T> list = new ArrayList<>();
	
	
	// method
	public List<T> getList() { // 넣은 다음에 꺼냄
		return list;
	}

	public void register(T item ) {
		list.add(item); // 리스트에 넣어줌
	}
	
	
	

}

 

=== Child_Executive(임직원용) ===

public class Child_Executive extends Employee {
	
	String cardNo; // 법인카드번호

	public Child_Executive(String userid, String passwd, String name, String jik, String cardNo) {
		super(userid, passwd, name, jik);
		this.cardNo = cardNo;
	}
	
	@Override
	public String toString() {
		
		return "== 임직원 ==\n"
				+ super.toString()
				+ "5. 법인카드번호: "+ cardNo + "\n";
	}
}

 

=== Child_Plain(평직원용) ===

public class Child_Plain extends Employee {

	int salary;
	
	public Child_Plain(String userid, String passwd, String name, String jik, int salary) {
		super(userid, passwd, name, jik);
		this.salary = salary;
	}
	
	public String toString() {
		
		return "== 평직원 ==\n"
				+ super.toString()
				+ "5. 연봉: "+ salary + "\n";
	}
}

 

=== Main ===

 

임원 정보 입력 후 출력

public class Main_2 {

	public static void main(String[] args) {
		
		Box_Employee<Child_Executive> box_executive = new Box_Employee<>();
		
		box_executive.register(new Child_Executive("hansk","1234","한석규","사장","1-01"));
		box_executive.register(new Child_Executive("dusk","5678","두석규","전무","2-01"));
		box_executive.register(new Child_Executive("sesk","0070","세석규","상무","3-01"));
		
		List <Child_Executive> executive_List = box_executive.getList();
		
		for(int i=0; i<executive_List.size(); i++) {
			System.out.println(executive_List.get(i));
		}
		/*
		 	== 임직원 ==
			1.아이디: hansk
			2.비밀번호: 1234
			3.성명: 한석규
			4.직급: 사장
			5. 법인카드번호: 1-01
			
			== 임직원 ==
			1.아이디: dusk
			2.비밀번호: 5678
			3.성명: 두석규
			4.직급: 전무
			5. 법인카드번호: 2-01
			
			== 임직원 ==
			1.아이디: sesk
			2.비밀번호: 0070
			3.성명: 세석규
			4.직급: 상무
			5. 법인카드번호: 3-01
		 */

평직원 정보 입력 후 출력

		Box_Employee<Child_Plain> box_plain = new Box_Employee<>();
		box_plain.register(new Child_Plain("leess","qwer","이순신","부장",8000));
		box_plain.register(new Child_Plain("eomjh","abcd","엄정화","과장",7000));
		box_plain.register(new Child_Plain("hongkd","1234","홍길동","대리",6000));
		
		List<Child_Plain> plain_list = box_plain.getList();
		
		for( Child_Plain plain : plain_list) {
			System.out.println(plain);
		}
		/*
		 	== 평직원 ==
			1.아이디: leess
			2.비밀번호: qwer
			3.성명: 이순신
			4.직급: 부장
			5. 연봉: 8000
			
			== 평직원 ==
			1.아이디: eomjh
			2.비밀번호: abcd
			3.성명: 엄정화
			4.직급: 과장
			5. 연봉: 7000
			
			== 평직원 ==
			1.아이디: hongkd
			2.비밀번호: 1234
			3.성명: 홍길동
			4.직급: 대리
			5. 연봉: 6000
		 */

 

섞어서 입력 후 출력

		Box_Employee<Employee> box_all = new Box_Employee<>();
		box_all.register(new Employee("superman","1234","슈퍼맨","부장"));
		box_all.register(new Child_Executive("batman","6789","배트맨","이사","4-01"));
		box_all.register(new Child_Plain("wonderwoman","0070","원더우먼","과장",9000));
		
		List<Employee> emp_list = box_all.getList();
		
		for(Employee emp : emp_list) {
			System.out.println(emp);
		}
		/*
		 	1.아이디: superman
			2.비밀번호: 1234
			3.성명: 슈퍼맨
			4.직급: 부장
			
			== 임직원 ==
			1.아이디: batman
			2.비밀번호: 6789
			3.성명: 배트맨
			4.직급: 이사
			5. 법인카드번호: 4-01
			
			== 평직원 ==
			1.아이디: wonderwoman
			2.비밀번호: 0070
			3.성명: 원더우먼
			4.직급: 과장
			5. 연봉: 9000
		 */
		
	} // end of main()