Java/Classes Relationships
동적 클래스 로딩 - 클래스 이름으로 클래스를 동적으로 로딩한다.
Steven J.S Min
2013. 10. 23. 21:04
java.lang.Class 클래스
Class 클래스를 이용하여 클래스를 동적으로 로딩 할수 있다.
인터페이스 클래스와 로딩할 클래스이다.
public interface Typewriter { public void typeLine(String s); .... }
|
public class Printer implements Typewriter { .... }
|
Class 클래스를 이용하여 클래스를 동적으로 로딩한다.
public class MyApplication { ... String outputDeviceName = "Printer";
try { // 로딩(Lookup)할 클래스를 지정한다. : Qualified 이름으로 지정할 수도 있다. Class newClass = Class.forName(outputDeviceName); // Typewriter 인터페이스를 구현한 디바이스 클래를 동적으로 로딩하여 사용 Typewriter device = (Typewriter)newClass.newInstance(); .... device.typeLine("Hello..."); } catch (Exception e) { ... } }
|