1. 程式人生 > >Java語法糖之內部類

Java語法糖之內部類

 

例1:

class Outer {
	public void md1(final int a) {
		final int b = 1;
		class LocalA {
			int c = a;
		}
		class LocalB extends LocalA {
			LocalB() {
				super();
			}
		}
	}
}

 

解語法糖後的形式如下:

 

class Outer {    
    Outer() {
        super();
    }    
    public void md1(final int a) {
        final int b = 1;
    }
}
 

class Outer$1LocalA {
	/* synthetic */ final Outer this$0;
	/* synthetic */ final int val$a;

	Outer$1LocalA(/* synthetic */ final Outer this$0, 
			      /* synthetic */ final int val$a) {
		this.this$0 = this$0;
		this.val$a = val$a;
		super();
	}

	int c = val$a;
}
 

class Outer$1LocalB extends Outer$1LocalA {
    /*synthetic*/ final Outer this$0;
    /*synthetic*/ final int val$a;
    
    Outer$1LocalB(/*synthetic*/ final Outer this$0, 
    		      /*synthetic*/ final int val$a) {
        this.this$0 = this$0;
        this.val$a = val$a;
        super(this$0, val$a);
    }
}

 

  

 

  

例2:

class Outer {
	public void md1(final int a,final int b) {
		class LocalA {
			int c = b;

			class LocalB {
				int d = a;
			}
		}
	}
}

 

 

class Outer {

	Outer() {
		super();
	}

	public void md1(final int a, final int b) {
	}
}


class Outer$1LocalA {
	/* synthetic */ final Outer this$0;
	/* synthetic */ final int val$a;
	/* synthetic */ final int val$b;

	Outer$1LocalA(/* synthetic */ final Outer this$0,
			      /* synthetic */ final int val$b,
			      /* synthetic */ final int val$a) {
		this.this$0 = this$0;
		this.val$b = val$b;
		this.val$a = val$a;
		super();
	}

	int c = val$b;
}


class Outer$1LocalA$LocalB {
	/* synthetic */ final Outer$1LocalA this$1;

	Outer$1LocalA$LocalB(/* synthetic */ final Outer$1LocalA this$1) {
		this.this$1 = this$1;
		super();
	}

	int d = this$1.val$a;
}

  

例3:

class Outer {
	public void md1(final int a) {
		class LocalA {
			int x1 = a;
		}
		
		class LocalB {
           Object x2 = new LocalA();
		}
	}
}

  

class Outer {
    
    Outer() {
        super();
    }
    
    public void md1(final int a) {
    }
}


class Outer$1LocalA {
    /*synthetic*/ final Outer this$0;
    /*synthetic*/ final int val$a;
    
    Outer$1LocalA(/*synthetic*/ final Outer this$0, 
    		      /*synthetic*/ final int val$a) {
        this.this$0 = this$0;
        this.val$a = val$a;
        super();
    }
    int x1 = val$a;
}

class Outer$1LocalB {
    /*synthetic*/ final Outer this$0;
    /*synthetic*/ final int val$a;
    
    Outer$1LocalB(/*synthetic*/ final Outer this$0, 
    		      /*synthetic*/ final int val$a) {
        this.this$0 = this$0;
        this.val$a = val$a;
        super();
    }
    Object x2 = new Outer$1LocalA(this$0, val$a);
}