InnerClass

import com.innerClass.test.Client.Contents;
public class Non_static_method_outerClass {
	
	public static void main(String[] args) {
		Client client = new Client();
		Contents c = client.new Contents();
		client.ship("XXX");
		/**
		 * error: The type Client.inner is not visible
		 */
		Client.inner in = client.new inner();
	}
}
class Client {
	class Contents {
		private int i =1;
		public int value() {
			return i;
		}
	}
	class Destination {
		private String lable;
		public Destination(String labe) {
			lable = labe;
		}
		String readLabe() {
			return lable;
		}
	}
	private class inner {
		
	}
	public Destination to(String s) {
		return new Destination(s);
	}
	public Contents contents() {
		return new Contents();
	}
	public void ship (String dest) {
		Contents c = contents();
		Destination d = to(dest);
		System.out.println(d.readLabe());
	}
}
/**
 *error: No enclosing instance of type Client is available due to some intermediate constructor invocation
 */
class ExtendsInnerClass extends Contents {
	
}
/**
 * error: No enclosing instance of type Client is available due to some intermediate constructor invocation
 *
 */
class ExtendsInnerClass2 extends Client.Contents {
	
}

正確的方式; java

/**
 * inherit Innerclass
 */
class ExtendsInnerClass3 extends Contents {
	public ExtendsInnerClass(Client c) {
		c.super();
	}
}
相關文章
相關標籤/搜索