如何在子类中访问超类的“protected static”变量,其中子类位于不同的包中。? [英] How can ‘protected static’ variable of superclass be accessed in the subclass, where subclass resides in different package..?

查看:104
本文介绍了如何在子类中访问超类的“protected static”变量,其中子类位于不同的包中。?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是同一问题的略微详细版本。

Here is the slightly elaborated version of the same question.

我们无法访问子类中受保护的变量(超类),其中子类位于不同的包中。我们只能访问supeclass的继承变量。但是如果我们将修饰符更改为'protected static',那么我们也可以访问超类的变量。为什么会这样。?

We cannot access protected variable (of the superclass) in the subclass,where subclass is in different package.We can access only the inherited variable of the supeclass. But if we change the modifier to ' protected static' then we can access the variable of the superclass too. Why is it like that.?

这是我试图解释的代码片段。

Here is the code snippet of the same which i was trying to explain.

package firstOne;

public class First {
    **protected** int a=7;
}

package secondOne;

import firstOne.*;

public class Second extends First {
    protected int a=10; // Here i am overriding the protected instance variable

    public static void main (String [] args){
        Second SecondObj = new Second();
        SecondObj.testit();
    }
    public void testit(){
        System.out.println("value of A in Second class is " + a);
        First b = new First();
        System.out.println("value in the First class" + b.a ); // Here compiler throws an error.
    }
}

上述行为是预期的。但我的问题是,如果我们将超类实例变量'a'的访问修饰符更改为'protected static',那么我们也可以访问变量(超类的变量)。我的意思是,

The above behavior is expected. But my question is, if we change the access modifier of the superclass instance variable 'a' to 'protected static' then we can access the variable(that of the superclass) too..! What i meant is,

package firstOne;

public class First {
    **protected static** int a=7;
}

package secondOne;

import firstOne.*;

public class Second extends First {
    protected int a=10;

    public static void main (String [] args){
        System.out.println("value in the super class" + First.a ); //Here the protected variable of the super class can be accessed..! My question is how and why..?
        Second secondObj = new Second();
        secondObj.testit();
    }

    public void testit(){
        System.out.println("value of a in Second class is " + a);
    }

}

以上代码显示输出:

超级7中的值

test1类中x的值是10

value of x in test1 class is 10

这怎么可能......?

How is this possible...?

推荐答案

从检查对受保护成员的访问权限 Java虚拟机:

From "Checking Access to Protected Members in the Java Virtual Machine":


m 成为在 c类中声明的成员属于包 p 。如果 m 是公共的,则可以通过(代码输入)任何类访问它。如果 m 是私有的,则只能通过 c 访问它。如果 m 具有默认访问权限,则只能由属于 p 的任何类访问它。

Let m be a member declared in a class c that belongs to a package p. If m is public, it can be accessed by (code in) any class. If m is private, it can be accessed only by c. If m has default access, it can be accessed only by any class that belongs to p.

如果 m 受到保护,事情稍微复杂一些。首先,属于 p 的任何类都可以访问 m
,就好像它具有默认访问权限一样。此外,它可以被属于不同于 p 的包的 c 的任何子类 s 访问,具有以下限制:if m 不是静态的,那么正在访问其成员的对象的类 o 必须是 s s的子类,写 o s (如果 m 是静态的,则限制不适用: m 可以是始终通过 s 进行访问。

If m is protected, things are slightly more complicated. First, m can be accessed by any class belonging to p, as if it had default access. In addition, it can be accessed by any subclass s of c that belongs to a package different from p, with the following restriction: if m is not static, then the class o of the object whose member is being accessed must be s or a subclass of s, written os (if m is static, the restriction does not apply: m can be always accessed by s).

我在 JLS,第6.6.2.1节,支持该部分关于正在访问其成员的对象必须是 s 或子类......。我没有看到任何支持静态子句的东西,但根据你自己的例子,它显然是真的。

I found that reference in the JLS, section 6.6.2.1, which supports the part about "the object whose member is being accessed must be s or a subclass...". I don't see anything supporting the static clause, but based on your own example, it's obviously true.

这篇关于如何在子类中访问超类的“protected static”变量,其中子类位于不同的包中。?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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