java 内部类的使用


调用内部类的某个方法,需要用new关键字创建外部类,然后在通过外部类去调用内部类。

代码如下:


public class OuterInnerClass {

	private String name;
	private String age;
	class innerClass{
		innerClass(){
			System.out.println("oox");
		}
	}
	public innerClass ooxx(){
		return new innerClass();
	}
	
	public static void main(String[] args) {
		OuterInnerClass out = new OuterInnerClass();
		OuterInnerClass.innerClass in = out.ooxx();
	}

	public String getName() {
		return name;
	}

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

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

}


调用内类中的方法:

public class OuterInnerClass {

	private String name;
	private String age;
	class innerClass{
		private String innername="test";
		innerClass(){
			System.out.println("oox"+this.innername);
		}
		public String getInnername() {
			return innername;
		}
		public void setInnername(String innername) {
			this.innername = innername;
		};
		
	}
	public innerClass ooxx(){
		return new innerClass();
	}
	
	public static void main(String[] args) {
		OuterInnerClass out = new OuterInnerClass();
		OuterInnerClass.innerClass in = out.ooxx();
		in.setInnername("mixian");
		System.out.println(in.getInnername());
		//OuterInnerClass out1 = OuterInnerClass.innerClasss();
	}

	public String getName() {
		return name;
	}

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

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

}



你可能感兴趣的:(java 内部类的使用)