1. 程式人生 > >私有內部類的外部呼叫方式

私有內部類的外部呼叫方式

話不多說,之間上程式碼:

interface InnerMethor{
	public void setInfo(String info) ;
	public String getInfo() ;
}

public class TestStaticInner {
	private String name;

	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	private Inner inner = new Inner();

	public Inner getInner() {
		if (inner != null) {
			return inner;
		}
		return new Inner();
	}
	private class Inner implements InnerMethor{
		private String info;

		public String getInfo() {
			return info;
		}

		public void setInfo(String info) {
			this.info = info;
		}
	}
}

public class MyThread implements Runnable{
	private int num;
	public void run() {
		TestStaticInner t=new TestStaticInner();
		t.setName(num+"");
		InnerMethor methor=t.getInner();
		methor.setInfo(num+"");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(t.getName()+"-------"+methor.getInfo());
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	
}

public class TestMain {
	public static void main(String[] args) {
		for(int i=0;i<10;i++){
			MyThread thread=new MyThread();
			thread.setNum(i);
			new Thread(thread).start();
		}
	}
}