简体   繁体   English

如何在子类中访问超类的“protected static”变量,其中子类位于不同的包中..?

[英]How can ‘protected static’ variable of superclass be accessed in the subclass, where subclass resides in different package..?

Here is the slightly elaborated version of the same question. 这是同一个问题的略微详细版本。

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. 我们无法访问子类中受保护的变量(超类),其中子类位于不同的包中。我们只能访问supeclass的继承变量。 But if we change the modifier to ' protected static' then we can access the variable of the superclass too. 但是如果我们将修饰符更改为'protected static',那么我们也可以访问超类的变量。 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.
    }
}

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..! 但我的问题是,如果我们将超类实例变量'a'的访问修饰符更改为'protected static',那么我们也可以访问变量(超类的变量)。 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);
    }

}

The above code shows the output: 上面的代码显示了输出:

value in the super class 7 超级7中的价值

value of x in test1 class is 10 test1类中x的值为10

How is this possible...? 这怎么可能...?

From "Checking Access to Protected Members in the Java Virtual Machine": 从“检查Java虚拟机中受保护成员的访问权限”:

Let m be a member declared in a class c that belongs to a package p . m是属于包p的类c中声明的成员。 If m is public, it can be accessed by (code in) any class. 如果m是public,则可以通过(code in)任何类访问它。 If m is private, it can be accessed only by c . 如果m是私有的,则只能通过c访问。 If m has default access, it can be accessed only by any class that belongs to p . 如果m具有默认访问权限,则只能由属于p的任何类访问它。

If m is protected, things are slightly more complicated. 如果m受到保护,事情会稍微复杂一些。 First, m can be accessed by any class belonging to p , as if it had default access. 首先, m可以被属于p的任何类访问,就像它具有默认访问权一样。 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 ). 此外,它可以被C属于与p不同的封装的任何亚类s中被访问,但有下列限制:·如果m不是静态的,则类ø其成员正在被访问必须是S或A的对象的S的子类中,书面ö≤ (如果m是静态的,则限制不适: 可被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...". 我在JLS第6.6.2.1节中找到了引用,它支持关于“正在访问其成员的对象必须是s或子类......”的部分。 I don't see anything supporting the static clause, but based on your own example, it's obviously true. 我没有看到任何支持静态子句的东西,但根据你自己的例子,它显然是正确的。

Override only applies to methods, not class variables. 覆盖仅适用于方法,而不适用于类变量。 There is no such thing as to override a variable. 没有覆盖变量的东西。 You are shadowing the symbol accessing variable a of the superclass. 您正在隐藏访问超类变量a的符号。

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

相关问题 当子类位于不同的包中时,为什么我的子类不能访问其超类的受保护变量? - Why can't my subclass access a protected variable of its superclass, when it's in a different package? 当子类在超类中受到保护时,子类如何使变量成为私有的? - How can a subclass make a variable private when it is protected in superclass? 是“继承”正确的术语来解释超类的静态方法可以通过子类访问吗? - Is “inherited” the correct term to explain static method of superclass can be accessed by subclass? 在子类中访问超类静态变量 - Access superclass static variable in subclass 为什么不同包中的子类无法通过超类实例访问其在另一个包中的超类的受保护字段? - Why a subclass in a different package is unable to access the protected fields of its superclass in another package through superclass instance? 如何访问子类中超类的受保护方法? - How to access a protected method of superclass in a subclass? 可以子类初始化超类变量 - can subclass initialize superclass variable 受保护的为什么不能在不同的包子类中访问? - protected can't access in different package subclass why? 获取给定超类的子类的静态变量 - Get static variable of subclass given superclass 子类引用变量如何指向超类对象 - How subclass reference-variable can point to superclass object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM