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) { ... }

}

 

 


'Java > Classes Relationships' 카테고리의 다른 글

어댑터로 사용된 Inner Class  (0) 2013.10.23
Interface vs Abstract  (0) 2013.10.18
Simple UML Tutorials  (0) 2013.02.12
Inheritance vs Composition  (0) 2013.02.09
Interface as Callback  (0) 2013.02.06
Posted by Steven J.S Min
,