Java 访问扩展抽象基类的类的实例的受保护变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18560341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:03:12  来源:igfitidea点击:

Accessing Protected Variable Of An Instance Of A Class That Extends An Abstract Base Class?

javaabstractprotectedaccess-modifiersbase-class

提问by Jan Tacci

I have an abstract class called MyAction which contains a protected enum variable. The class is defined as follows:

我有一个名为 MyAction 的抽象类,它包含一个受保护的枚举变量。该类定义如下:

package mypackage;

public abstract class MyAction {
    public enum ActionId {
        ACTION1, ACTION2;
    }

    protected ActionId actionId;

    // constructor
    public MyAction(ActionId actionId) {
        this.actionId = actionId;
    }

    public ActionId getActionId() {
        return actionId;
    }    
    ...
    ...
}

I created a specific action, MyAction1, that extends MyAction:

我创建了一个扩展 MyAction 的特定操作 MyAction1:

package mypackage;

public class MyAction1 extends MyAction {
    public MyAction1() {
        super(ActionId.ACTION1);
    }
    ...
    ...
}    

I have a singleton utility class (in the same package) that creates an instance of MyAction1 and stores it in a HashMap:

我有一个单例实用程序类(在同一个包中),它创建了 MyAction1 的实例并将其存储在 HashMap 中:

package mypackage;

public class MyActionFactory {
    private static MyActionFactory theInstance;
    private HashMap<ActionId, MyAction> actions;

    private MyActionFactory() {
        actions = new HashMap<ActionId, MyAction>();
        MyAction1 myAction1 = new MyAction1();
        actions.put(myAction1.actionId, myAction1); // able to access protected variable actionId
    }

    public static VsActionFactory getInstance() {
        if (theInstance == null)
            theInstance = new VsActionFactory();
        return theInstance;
    }    
    ...
    ...
}

Note that in the method actions.put(myAction1.actionId, myAction1)I am able to access the protected member actionId.

请注意,在方法actions.put( myAction1.actionId, myAction1) 中,我能够访问受保护的成员actionId

Why is it that I can access the protected member actionId(contained in the base class MyAction) of the instance of MyAction1? I thought protected members were only accessible to subclasses.

为什么我能访问受保护的成员actionId(包含在基类MyAction的实例)MyAction1?我认为受保护的成员只能被子类访问。

Does it have anything to do with MyActionFactorybeing in the same package as the others?

它与MyActionFactory与其他包在同一个包中有什么关系吗?

采纳答案by Martijn Courteaux

The protectedkeyword makes things visible within the same package. Which is the case, because both of your classes are in package mypackage.

protected关键字使事情在同一封装内可见。情况就是这样,因为您的两个课程都在package mypackage.

Here is a nice table, taken from Oracle.com:

这是一个不错的表格,取自Oracle.com

enter image description here

在此处输入图片说明

回答by Malwaregeek

Protected access modifier allows access in the same package + sub classes in other packages.You can remember it as default access plus inheritance.

受保护的访问修饰符允许在同一个包+其他包中的子类中进行访问。您可以将其记住为默认访问加继承。