public class TestMemberInner {
public static void main(String[] args){
Outer outer = new Outer();
//outer.get();//--->通过外部类访问,get方法中创建了内部类的实例
//Outer.get(1);//--->静态访问
Outer.Inner in = outer.getInner();
//Outer.Inner in = outer.new Inner();
in.get();//常用方式,在其他类中通过共有方法得到内部类的实例。
//outer.getInner();
}
}
class Outer{
private int num = 3;
public static int index = 4;
/*private */ class Inner{//getInner()在外部类可以用,但是Outer.Inner不可见了
int num = 5;
//static int index = 6; -----> error!
public void get(){
int num = 6;
System.out.println(num);
System.out.println(this.num);
System.out.println(Outer.index);
System.out.println(Outer.this.num);
}
}
public Inner getInner(){//如果内部类是私有的,此例下私有内部类,其他类得不到内部类的实例。
System.out.println(new Inner());
return new Inner();
}
public void get(){
System.out.println(num);
Inner in = new Inner();
in.get();
System.out.println("this is outer get !");
}
public static void get(int i){
Outer outer = new Outer();
System.out.println(outer.num);
Outer.Inner in = outer.new Inner();//跟下边的写法相同,下边的是详细写法
//Outer.Inner in = new Outer().new Inner();
in.get();
//Outer.Inner in = new Outer.Inner() ---------> error!
System.out.println("this is outer static get !");
}
}
/*
public class TestMemberInner2{
public static void main(String[] args){
//1.获得一个Inner的对象
Outer o = new Outer();
//Object in = o.getInner();
A in = o.getInner();
//2.用Inner对象调用get()
in.get();
}
}
interface A {
void get();
}
class Outer {
private class Inner implements A {//extends Object 通过接口实现了其他类访问私有内部类。还是体现了接口的好处
public void get(){
System.out.println("Hello World ! ");
}
}
/*public Object getInner(){
return new Inner();
}*/
public A getInner(){
return new Inner();
}
}
*/
1.利用方法的返回值。
2.参数往方法里传,返回值往方法外传