Java - 无法从子类访问受保护的字段? [英] Java - Protected field not accessible from the subclass?

查看:35
本文介绍了Java - 无法从子类访问受保护的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Java 访问修饰符.为此,我创建了一个 Machine 类:

I am in process of learning the Java access modifiers. For that, I have created a class Machine:

package udemy.beginner.interfaces;

public class Machine {

    public String name;
    private int id;
    protected String description;
    String serialNumber;

    public static int count;

    public Machine(){
        name = "Machine";
        count++;
        description = "Hello";
    }

}

然后,在另一个包中,我创建了一个类Robot作为汽车Machine的子类:

Then, in another package, I have created a class Robot as a subclass of a car Machine:

package udemy.beginner.inheritance;

import udemy.beginner.interfaces.Machine;

public class Robot extends Machine {

    public Robot(){

        Machine mach1 = new Machine();
        String name = mach1.name;
        //here I am getting error "The field Machine.description is not visible" 
        String description = mach1.description; 
    }

}

在尝试访问 Robot 类中的字段 description 时出现错误.根据我对 protected 访问修饰符如何工作的理解,它应该没问题,但也许我搞砸了一些事情.有什么想法吗?

I am getting an error when trying to access the field description in the class Robot. From my understand of how protected access modifier works, it should be OK though, but maybe I messed up something. Any thoughts?

我试图将 Robot 类移动到与 Machine 类所在的包相同的包中,现在它可以工作了,无需使用它.如果有人可以解释我this.根据下面的答案,它应该不能正常工作......

I have tried to move Robot class to the same package as Machine class is in and now it works, without a need to use this. If someone can explain me this. According to the answers below, it should not work as well ...

推荐答案

您不能在类的不同实例中访问受保护的超类字段.

You can't access a protected superclass field in a different instance of the class.

有一个很好的理由:您不知道它是否与您具有相同的子类,或者完全不同的子类.如果允许访问受保护的字段,您将被允许访问完全不相关的类的内部.

There's a good reason: you don't know whether it has the same subclass as yourself, or a completely different subclass. If it were allowed to access the protected field, you would be allowed to access the internals of entirely unrelated classes.

如果您确定该对象与要访问超类字段的类属于同一子类,则可以对该对象进行强制转换;当您这样做时,您可以访问受保护的字段.

If you are sure that the object is of the same subclass as the class that wants to access the superclass field, you can cast the object; when you that, you can access the protected field.

规则在Java 语言规范第 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.

6.6.2.1.访问受保护的成员

设 C 为声明受保护成员的类.访问是仅允许在 C 的子类 S 的主体内使用.

Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.

另外,如果Id表示实例字段或实例方法,则:

In addition, if Id denotes an instance field or instance method, then:

  • 如果访问是通过限定名 Q.Id 进行的,其中 Q 是一个ExpressionName,则当且仅当类型为表达式 Q 是 S 或 S 的子类. [这是相关部分]

这篇关于Java - 无法从子类访问受保护的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆