简体   繁体   English

从子类访问受保护的超类成员

[英]Access protected super-class member from sub-class

I'm reading this Java SCJP book and I came across this: 我正在阅读这本Java SCJP书,并且发现了这一点:

The protected and default access control levels are almost identical, but with one critical difference. 受保护的访问控制级别和默认的访问控制级别几乎相同,但有一个关键的区别。 A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package. 仅当访问成员的类属于同一包时,才可以访问默认成员,而即使子类在不同的包中,子类也可以(通过继承)访问受保护的成员。

So I decided to test out the protected point. 因此,我决定测试protected点。

I have a super-class in a package 我的包裹里有超一流的

package scjp;

public class Token {

    protected int age = 6; //This is the protected class-level variable.

    public Token(String name){
        this.name = name;
    }
    public Token(String name, int age){
        this.name = name;
        this.age = age;
    }

    public String getName(){
        return this.name;
    }

    public int getAge(){
        return this.age;
    }
}

The I have a sub-class in another package; 我在另一个包中有一个子类;

package pack;

import scjp.Token;

public class son extends Token{

    public static void main(String[] args) {
       System.out.println(Token.age);
    }

}

As you can see I'm trying to access the protected class-level integer variable age in the super-class. 如您所见,我正在尝试访问超类中受保护的类级整数变量age

But I get this error: 但是我得到这个错误:

 age has protected access in scjp.Token
        at pack.son.main(son.java:11)
Java Result: 1

So, what's going wrong? 那么,怎么了?

You are trying to access a non static protected member of your super class with class reference.. 您正在尝试使用类引用访问父类的非静态保护成员。

public static void main(String[] args) {
       Token t = new Token("somehting");
       System.out.println(t.age);
    }

will compile and work fine. 将编译并正常工作。

age is not a static variable in your class Token . age不是您类Token静态变量 its an instance non-static member and you access instance non-static memebers using the instance of your class . 实例非静态成员,并且您可以使用实例访问实例非静态成员

   System.out.println(new Token().age);

should work 应该管用

And also as your Son IS-A Token you should be able to access it directly unless you have another variable with the same name(shadowing) in your Son class in which case it would access the 'age' declared in Son, as i can see you have not declared age in your Son class you could also directly access it like this: 并且作为您的Son IS-A Token您应该能够直接访问它,除非您在Son类中具有另一个具有相同名称(阴影)的变量,在这种情况下,它将访问Son中声明的“年龄”,如我所能看到您尚未在Son类中声明age ,您也可以像这样直接访问它:

System.out.println(age);

age is not a static member. age不是静态成员。 You will need to access it using an instance of the class Token . 您将需要使用Token类的实例访问它。 Not the class name itself. 不是类名本身。

You are misunderstood the concept 您误解了这个概念

whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package. 子类可以访问(通过继承)受保护的成员,即使该子类位于其他程序包中也是如此。

Means you can access the member by extending it in your class but you are try to access variable in your class through static concept even your variable is not static So i think you have to take a look of basics java. 意味着您可以通过在类中扩展它来访问该成员,但是您尝试通过静态概念访问类中的变量,即使您的变量不是静态的,所以我认为您必须看一下Java的基础知识。 Means while you can fix your program in two way. 意味着您可以用两种方式修复程序。

1. create object of your class and access your protected variable for that you have to create a constructor which should call parent constructor . 1.创建您的类的对象并访问受保护的变量,为此您必须创建一个构造函数,该构造函数应调用父构造函数。 Your son class may be look like following . 您的儿子班级可能看起来像是跟随。

public son(String name) {
    super(name);
    // TODO Auto-generated constructor stub
}

public static void main(String[] args) {
    son s = new son("Hello"); //$NON-NLS-1$
   System.out.println(s.age);
}

2. you can get access by making your protected variable as static and use it from your class like following. 2.您可以通过将您的受保护变量设置为静态来访问,并在类中使用它,如下所示。
Token class: 代币类别:

public class Token {
   protected static int age = 6; //This is the protected class-level variable.
  ....
}

son class: 儿子班:

  System.out.println(son.age);

You can't access a static member, if you want to test inheritance you can do as following 您无法访问静态成员,如果要测试继承,可以执行以下操作

public static void main(String[] args) {
   son s = new son();
   System.out.println(s.age);
}

You shouldn't use name of public class begin with a lowcase letter. 您不应使用以小写字母开头的公共类名称。

Protected Member : In other words, protected = Inheritance 受保护的成员:换句话说, protected =继承

For a subclass outside the package, the protected member can be accessed only through inheritance . 对于包外部的子类, 只能通过继承访问受保护的成员。 The subclass cannot use dot operator on the superclass reference to access the protected member. 子类不能在超类引用上使用点运算符来访问受保护的成员。

you're not accessing it from the subclass. 您不是从子类访问它。 You're accessing it from main().The "main" method isn't a member of either class, but it's trying to directly access the member variable. 您正在从main()访问它。“ main”方法不是任何一个类的成员,但是它试图直接访问成员变量。

public class son extends Token{
    public static void main(String[] args) {
       System.out.println(Token.age); // Error
    }
}

Create non-static method and inherit the protected member. 创建非静态方法并继承受保护的成员。

public class son extends Token{
   public void test1() {
     System.out.println(age); // 6
     System.out.println(new Token().age); // 6 This also works.
   }
}

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

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