日期:2014-05-20  浏览次数:20638 次

纠正Java静态内部类的谣言
我看到很多网上说,this指向实例类!所以,静态内部类能使用this。
我不知道为什么别忍着要这么说,我觉着这话错了,别的不说,上代码,
如果我错了,求指正,勿喷!

public class A {
private static class B{
public int a ;
public int b ;
public B a(int a){ this.a = a ; return this;}
public B b(int val){ b = val ; return this;}
}
public static void main(String[]args){
B a = new B();
B b = new B();
a.a = 1 ;
b.a = 2 ;
System.out.println(a.a+":"+b.a);
B c ;
c = a.a(3).b(5);
System.out.println(a.a+":"+b.a+":"+a.a+":"+c.b);
}
}


很想然,我的静态内部类B使用了this!

可能别人说静态内部类不能有this深有它意,我不懂的咯。
如果我错了,求指正,勿喷!

欢迎观摩此帖!

------解决方案--------------------
老实说,我看不出你的代码有何不妥/特别和你想表达什么

------解决方案--------------------
说的是static 方法/代码块里不能使用this。怎么变成static类里了?
------解决方案--------------------
静态内部类是可以用this的,但静态方法,静态块是不能用的。
------解决方案--------------------
public static B a(int a){ 
            this.a = a ; return this;}
        public B b(int val){
            b = val ; return this;}
    }   
这里你如果这么写就是不对了。