220802 오늘의 훈련 / 리플렉션

(。◠ ◠。)·2022년 8월 2일

국비 개발 훈련 일기

목록 보기
12/35
		WorkerValue wv = new WorkerValue("홍길동", WorkerValue.POSITION_MANAGER);
		//워커벨류에대한 클레스 정보를 담은 클레스 타입 변수를 만들었다.
		Class<?> clazz1 = WorkerValue.class;
		//위에꺼랑 같은 코드
		Class clazz2 = wv.getClass();
		Class clazz3 = null;
		
		//이것도 같은거
		try {
			clazz3=Class.forName("reflex.WorkerValue");
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		System.out.println(System.identityHashCode(clazz1));
		System.out.println(System.identityHashCode(clazz2));
		System.out.println(System.identityHashCode(clazz3));
		System.out.println(System.identityHashCode(null));
		Class clazz = WorkerValue.class;
		
		Constructor[] constructors = clazz.getConstructors();
		System.out.println("====constructors=====");
		for(int i = 0; i < constructors.length;i++) {
			System.out.println(constructors[i].toString());
		}
		
//		Method[] Methods = clazz.getMethods();
		//private 안나옴 상속받은 애들 나옴
		Method[] Methods = clazz.getDeclaredMethods();
		//private나옴 상속받은 애들 안나옴
		System.out.println("====Method=====");
		for(int i = 0; i < Methods.length;i++) {
			System.out.println(Methods[i].toString());
		}
		
		Field[] fields  = clazz.getFields();
		System.out.println("====fields=====")	;
		for(int i = 0; i < fields.length;i++) {
			System.out.println(fields[i].toString());
		}
		WorkerValue wv = new WorkerValue("홍길동", WorkerValue.POSITION_MANAGER);
		Class clazz = wv.getClass();
		
		try {
			
			Object obj = clazz.getConstructor().newInstance();
			if(obj instanceof WorkerValue) {
				System.out.println("이건 워커벨류 클레스 오브젝트임");
				
				WorkerValue wv2 = (WorkerValue)obj;
				wv2.setName("임꺽정");
				wv2.setPosition(WorkerValue.POSITION_EMPLOYEE);
				System.out.println(wv2.toString());
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		//B클레스 타입의 클레스 생성
		Class<?> aCls = B.class;
		//B클레스의 생성자를 취득한다
		Constructor<?> constroctor = aCls.getConstructor();
		
		//newInstance()를 호출하여 B 인스턴스를 생성한다
		B b = (B)constroctor.newInstance();
		System.out.println(b.f());
profile
화이탱!

0개의 댓글