简体   繁体   English

为什么静态嵌套类不能访问外部类的“ this”指针?

[英]Why can't a static nested class access “this” pointer of outer class?

在此处输入图片说明

The thing that bothers me is the second point. 第二点是困扰我的事情。

I thought it might have to do with the fact that the "this" pointer isn't static and so the inner class can't access it. 我认为这可能与“ this”指针不是静态的,因此内部类无法访问它有关。 I'm not sure if that's the right explanation though. 我不确定这是否是正确的解释。

This also raised another question for me which was "where is the "this" pointer defined?" 这对我也提出了另一个问题,即“在哪里定义了“ this”指针?”

The difference between a static nested class and one that isn't static is precisely that an instance of a non- static inner class is associated with a specific instance of the enclosing class, while a static inner class isn't. 一个之间的差static嵌套类和一个不是static恰恰是一个非的一个实例static内部类与封闭类的特定实例相关联,而一个static内部类是没有的。 There is no A.this for an instance of a static inner class to be associated with. 没有 A.this要的实例static内部类要与之关联。

If an inner class is static then it can be instantiated without an enclosing instance of the outer class. 如果内部类是static则可以实例化它而无需包含外部类的实例。 For a non- static inner class an instance of the outer class is required for instantiation. 对于非static内部类,实例需要外部类的实例。 For example, if your class structure is this : 例如,如果您的类结构是这样的:

public class A {
    public static class B {
    }

    public class C {
    }
}

then to instantiate B and C you would have to do it like this : 然后要实例化BC您必须像这样:

// simply
A.B b = new A.B();
// however
A.C c = new A().new C();

Behind the scenes when a non- static inner class is instantiated an instance of the enclosing class is passed into the constructor. 当实例化非static内部类时,在幕后将封闭类的实例传递给构造函数。 The instance of OuterClass.this is then accessible because of this. 因此,可以访问OuterClass.this的实例。

To verify the "behind the scenes" thing you can check the declared constructors and declared fields of the inner class via reflection: 要验证“幕后”,您可以通过反射检查内部类的声明的构造函数和声明的字段:

// prints that there is one field of type A called "this$1"
for (Field f : A.C.class.getDeclaredFields()) {
    System.out.println(f);
}

// prints that the constructor takes in one parameter of type A
for (Constructor c : A.C.class.getDeclaredConstructors()) {
    System.out.println(c);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 嵌套类AsyncTask无法修改外部类静态对象 - Nested class AsyncTask can't modify Outer Class static objects 编译外部类时静态嵌套类的.class文件不能通过关联使用,但可以通过继承使用。 为什么? - .class file of static nested class on compiling Outer class can't be used via association but can be used via inheritance. why? 静态嵌套类可以访问外部类的私有构造函数 - Static nested class has access to private constructor of outer class 静态嵌套类是否可以完全访问私有外部类成员? - Static nested class has full access to private outer class members? 为什么这个静态内部类不能在其外部类上调用非静态方法? - Why can’t this static inner class call a non-static method on its outer class? 为什么类不能扩展其中发生的静态嵌套类? - Why can't a class extend a static nested class occurring within it? 为什么内部 class 无法访问外部 class 方法,这是正常方法和 static 方法之间的过载? - Why inner class can not access outer class method which is overload between normal method and static method? 内部静态类继承内部静态类,子级的外部类也继承父级的外部类,为什么我不能进行强制转换 - Inner static class inherits inner static class, and the outer class of the child also inherits the outer class of the parent, why I can't do casting 无法访问外部类修饰符(java) - can`t access outer class modifiers (java) 在外部类中实现静态嵌套类的接口 - Implement static nested class' interface in outer class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM