import org.junit.Test; public class ClassKindTestCase { public interface ParentClass { public void add(); } @Test public void innerClassTest(){ innerLocalClass("ddakker", 21); innerAnonymousClass("ddakker", 22); } // 로컬 클래스 public void innerLocalClass(final String name, final int age){ class ChildClass implements ParentClass { @Override public void add(){ System.out.println("[innerLocalClass] name=" + name + ", add age=" + age); } } ParentClass instance = new ChildClass(); executeMethos(instance); } // 익명 내부클래스 public void innerAnonymousClass(final String name, final int age){ /*ParentClass instance = new ParentClass(){ @Override public void add() { System.out.println("[innerAnonymousClass] name=" + name + ", add age=" + age); } }; executeMethos(instance);*/ executeMethos( new ParentClass(){ @Override public void add() { System.out.println("[innerAnonymousClass] name=" + name + ", add age=" + age); } } ); } // 실행(공통 분리) public void executeMethos(ParentClass object){ object.add(); } }
내부 클래스(Inner Class
2013. 2. 12. 21:53