171113 LocalClass - RYUDONGJIN/Memo_wiki GitHub Wiki

LocalClass

  • 로컬클래스
public class practice  {
    
	public static void main(String[] args) {
		
		OuterClass oc1 = new OuterClass("First");
		Readable lc1 = oc1.createLocalClassInst(1);
		lc1.read();
		
		OuterClass oc2 = new OuterClass("Second");
		Readable lc2= oc2.createLocalClassInst(2);
		lc2.read();
	}	
}

class OuterClass {
	private String myName;
	
	OuterClass(String name){
		myName = name;
	}
	
	public Readable createLocalClassInst(final int instID) {
		class LocalClass implements Readable {
			public void read() {
				System.out.println("Outer inst name: " + name);
				System.out.println("Local inst ID: " + instID);
			}
		}
		return new LocalClass();
	}
}

interface Readable {
	public void read();
}