简体   繁体   English

在私有内部类的字段的访问修饰符中是否有任何意义?

[英]Is there any sense in access modifiers for fields of the private inner class?

Suppose I have an outer class with an inner class inside. 假设我有一个带有内部类的外部类。 The inner class has four fields with all possible access modifiers. 内部类有四个字段,包含所有可能的访问修饰符。

class Outer {
    private class Inner {
        public int publicField;
        protected int protectedField;
        int packagePrivatefield;
        private int privateField;
    }

    void doSomethingWithFields() {
        Inner inner = new Inner();
        inner.publicField = 111;
        inner.protectedField = 111;
        inner.packagePrivatefield = 111;
        inner.privateField = 111;
    }
}

The inner class is private, so I can't create instances of it outside the Outer class. 内部类是私有的,所以我不能在Outer类之外创建它的实例。 Inside the Outer class, if I create an instance of the inner class and try to change the value for each of the fields I will succeed to do that. 在Outer类中,如果我创建内部类的实例并尝试更改每个字段的值,我将成功地执行此操作。 So I see that there is no sense in access modifiers for the above fields. 所以我看到上述字段的访问修饰符没有任何意义。 Is there? 在那儿?

Edited: The main question is: Which access modifiers should I choose for members of the private inner class? 编辑:主要问题是: 我应该为私人内部成员选择哪些访问修饰符? Inner class can not only implement interface. 内部类不仅可以实现接口。 I can put some difficult structure with logic into it. 我可以把一些带有逻辑的困难结构放进去。

Access modifiers have a say in inheritance. 访问修饰符在继承中有发言权。 Another inner private class that extends that class you talk of does not have the privileges of the outer class. 扩展您所谈论的类的另一个内部私有类没有外部类的特权。

public class Main {
    private class Test {
        protected int hello;
    }
    private class TestNext extends Test {
        private TestNext() {
            this.hello = 1;
        }
    }
}

Will compile, but if hello was private, it would not. 将编译,但如果hello私有,它不会。

Right, if the inner class is private it doesn't make sense. 是的,如果内部类是私有的,那就没有意义了。

But keep in mind that inner classes are not very beautiful in general, you should rather group multiple classes with packages. 但请记住,内部类通常不是很漂亮,您应该使用包将多个类组合在一起。

Yes. 是。 You are correct. 你是对的。 But only if you have single nested inner There is no different between having various access modifiers in a private inner class. 但是只有你有单一的嵌套内部在私有内部类中拥有各种访问修饰符之间没有区别。 Nothing can be seen out of the class. 课堂上没有任何东西可以看出来。 But if you have another nested inner class then your access modifiers will matter. 但是如果你有另一个嵌套的内部类,那么你的访问修饰符就很重要了。 eg 例如

class A{
   private class B{
      class C{}
   }
}

in this case class B's field and methods access modifiers will matter 在这种情况下,B类的字段和方法访问修饰符将很重要

If the inner class does not implement any Interface or inheritance, then there is no point of specifying access modifiers. 如果内部类没有实现任何接口或继承,那么就没有必要指定访问修饰符。

If the inner class inherits something, it does make sense in that context. 如果内部类继承了某些内容,那么它在该上下文中确实有意义。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM