ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 18. Class 클래스
    개발자 수업/Java 2021. 10. 14. 12:20

    1. Class 클래스
        1) 자바의 모든 클래스와 인터페이스는 컴파일 후 .class 파일이 생성됨
        2) Class 클래스는 컴파일된 class 파일을 로드하여 객체를 동적 로드하고, 정보를 가져오는 메서드가 제공됨
        3) Class.forName("클래스 이름") 메서드로 클래스를 동적으로 로드 함
            - 실행(runtime)중에 데이터 타입을 binding하는 방법
            - 동적 로딩 시 오류가 발생하면 프로그램의 심각한 장애 발생가능 (컴파일 시에 타입이 정해지지 않으므로)
        4) reflection 프로그래밍
            - Class 클래스를 사용하여 클래스의 정보(생성자, 변수, 메서드) 등을 알 수 있고 인스턴스를 생성하고, 메서드를 호출하는 방식의 프로그래밍

     

    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;
    
    public class ReflectionTest {
    
    	public static void main(String[] args) throws ClassNotFoundException {
    		
    		Class clazz = Class.forName("java.lang.String");
    		
    		Constructor[] constructors = clazz.getConstructors();
    		for(Constructor constructor : constructors) {
    			System.out.println(constructor);
    		}
    		
    		System.out.println();
    		
    		Method[] methods = clazz.getMethods();
    		for(Method method : methods) {
    			System.out.println(method);
    		}
    	}
    }

    public class Person {
    	
    	private String name;
    	private int age;
    	
    	public Person() {}	// 기본 생성자
    
    	public Person(String name) {
    		// super();
    		this.name = name;
    	}
    
    	public Person(String name, int age) {
    		// super();
    		this.name = name;
    		this.age = age;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    	
    	@Override
    	public String toString() {
    		
    		return name;
    	}
    	
    }
    import java.lang.reflect.Constructor;
    import java.lang.reflect.InvocationTargetException;
    
    public class ClassTest {
    	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
    		// 1. new 연산자 class 생성
    		Person person = new Person("Park");
    		System.out.println(person);		// Park
    		System.out.println();
    		
    		// 2. forName()
    		Class clazz = Class.forName("kr.co.ezenac.classs.Person");
    		Person person2 = (Person)clazz.newInstance();
    		person2.setName("Lee");
    		System.out.println(person2);	// Lee
    		
    		// 3. 
    		Class clazz2 = person.getClass();
    		Person person3 = (Person)clazz2.newInstance();
    		System.out.println(person3);	// null
    		
    		Class[] parameterTypes = {String.class};
    		Constructor constructor = clazz2.getConstructor(parameterTypes);
    		
    		Object[] arguments = {"Kim"};
    		Person person4 = (Person)constructor.newInstance(arguments);
    		System.out.println(person4);	// kim
    	}
    }

    '개발자 수업 > Java' 카테고리의 다른 글

    20. Generic 프로그래밍  (0) 2021.10.14
    19. Wrapper 클래스  (0) 2021.10.14
    17. String 클래스  (0) 2021.10.13
    16. Object 클래스  (0) 2021.10.13
    과제2 - CarTest  (0) 2021.10.13

    댓글