简体   繁体   English

父类可以看到子类的受保护变量吗?

[英]Can the parent class see a child classes protected variables?

I'm very fuzzy on how inheritance works and just want to make sure that I'm looking in the right direction. 我对继承的工作方式非常模糊,只想确保我朝着正确的方向看。

From my understanding I understand that 据我了解,

  1. public variables are accessible by all packages, 所有包都可以访问公共变量,
  2. default within the single package, 单个包中的默认值,
  3. private within only the class, 仅在班级内私有
  4. and protected with sub-classes. 并受子类保护。

I know that child classes can see parent class's protected variables. 我知道子类可以看到父类的受保护变量。

My question: Does it work the other way around? 我的问题:它是否反过来起作用?

Does it work the other way around? 它反过来起作用吗?

No, not if they're not in the same package. 不,如果它们不在同一包装中,则不会。 According to the table in the Java access control tutorial , protected exposes the member to subclasses and other classes in the same package, not superclasses: 根据Java访问控制教程中的表格, protected将成员暴露给同一包中的子类和其他类,而不是超类:

\n                  Access Levels 访问级别\n+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ +-----------------+\n| | Modifier Class Package Subclass World | 修饰符类包子类世界|\n| | public YYYY | 公开YYYY |\n| | protected YYYN | 受保护的YYYN |\n| | no modifier YYNN | 没有修饰符YYNN |\n| | private YNNN | 私人YNNN |\n+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ +-----------------+\n

And from the JLS : JLS

6.6.2. 6.6.2。 Details on protected Access 受保护访问的详细信息

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object. 对象的protected成员或构造函数只能从负责该对象实现的代码在其中声明该对象的包外部访问。

The superclass isn't responsible for the implementation of the subclass object. 超类不负责子类对象的实现。

You can test it yourself: 您可以自己测试:

b/Base.java : b/Base.java

package b;

import a.Example;

public class Base {
    public static void showAnswer(Example e) {
        System.out.println(e.answer); // 
    }
}

a/Example.java : a/Example.java

package a;

import b.Base;

public class Example extends Base
{
    protected int answer = 42;

    public static void main(String[] args)
    {
        Example e = new Example();
        Base.showAnswer(e);
    }
}

Trying to compile that fails with: 尝试编译失败的原因:

\n./b/Base.java:7: error: answer has protected access in Example ./b/Base.java:7:错误:示例中的答案已保护访问\n        System.out.println(e.answer); System.out.println(e.answer); // // \n                            ^ ^\n1 error 1个错误\n

If you think about it, what you are asking does not make too much sense. 如果您考虑一下,您的要求没有太大意义。 Consider this: 考虑一下:

public class A {
    public void doSomething() {
        // This is not actually possible!!!
        bField = 12;
    }
}

public class B extends A {
    protected int bField;
}

public class C extends A {
     // ... whatever except bField...
}

...
A aInstance = new C();
// What should happen here? a C does not have a bField!!!
aInstance.doSomething();

在此处输入图片说明

As you see its possible from subclasses not superclasses 如您所见,它可能来自子类而不是超类

You can read offical documentation here 您可以在此处阅读官方文档

Does it work the other way around? 它反过来起作用吗?

No unless they are in the same package 否,除非它们在同一包装中

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

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