Java 多级继承 - 级别 1 类中的受保护实例变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13241196/
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-10-31 11:59:50  来源:igfitidea点击:

Java Multilevel Inheritance - Protected Instance Variable in Level 1 Class

javaclassinheritance

提问by Dhruvenkumar Shah

I have an question which comes under multilevel inheritance in Java. All Three Classes are in Same package

我有一个问题,它属于 Java 中的多级继承。所有三个类都在同一个包中

I have class A :

我有 A 类:

public class A {
   protected int x;
}

public class B extends A {
   public void doSomething {
      // x is visible.agreed, as it is a direct subclass of A
   }  
}


public class C extends B {
   public void doSomething {
      // x is still visible, how come? I mean it is at the 2nd level 
     //  I am confused why?
   }  
}

does it like have any significance? or it is behavior which we have to take it by default?

它喜欢有什么意义吗?或者这是我们必须默认采取的行为?

回答by PermGenError

variable/methods marked with protectedmodifier are visible to all the classes in the same pacakage and only to subclasses in different packages. below are the example cases.

标有protected修饰符的变量/方法对同一包中的所有类可见,并且仅对不同包中的子类可见。下面是示例案例。

package a;
class A{
protected int x;
}
class B extends A{
//x can be accessed from this class
} 

class C extends B {
//x can be accessed from this class
}
class D{
//x can be accesed this class but you will have to create A's instance 
}

package B 
class One {
//cannot access x from this class
}
class Two extends A {
//can access x from this class
}

回答by Ted Hopp

The access level modifiers in Java are:

Java 中的访问级别修饰符是:

  • public- visible to all code
  • protected- visible to all code in the same package and to subclasses regardless of package
  • nothing(default) - visible to all code in the same package
  • private- visible only to code in the same class (including nested classes)
  • public- 对所有代码可见
  • protected- 对同一包中的所有代码以及子类可见,无论包如何
  • (默认)- 对同一包中的所有代码可见
  • private- 仅对同一类中的代码可见(包括嵌套类)

See, for instance, the Java tutorial Controlling Access to Members of a Classor (for lots of technical details) section 6.6 of the Java Language Specification.

例如,请参阅 Java 教程Controlling Access to Members of a Class或(有关大量技术细节)Java Language Specification 的第 6.6 节

回答by Amir Afghani

Definition of the keyword

关键字的定义

Protected

受保护

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

protected 修饰符指定该成员只能在其自己的包中访问(与包私有一样),此外,还可以由另一个包中其类的子类访问。

I suggest you read this.

我建议你阅读这个

回答by Eric

The Java protectedkeyword works its way down to allsubclasses (and members of the package). Ifthere was a protectedmember in Object, any object could access it. By contrast, privateis only visible to the local class (and inner classes), and publicis accessible by all.

Javaprotected关键字适用于所有子类(和包的成员)。如果 中protected成员Object,则任何对象都可以访问它。相比之下,private仅对本地类(和内部类)可见,并且public可供所有人访问。

Have a look at this glossary, which shows in-depth how protectedmembers and methods are inherited, and the Java documentation on inheritance.

看看这个词汇表,它深入展示了protected成员和方法是如何继承的,以及关于继承Java 文档

回答by Tomasz Nurkiewicz

You cannot restrict access to members in subclasses. This rule applies to methods (you can't override publicmethod and make it private) but you can see analogy here. If the field is protected, it will be protectedin every subclass, no matter how deep. Of course you can't override fields in Java, but just to give you an overview.

您不能限制对子类中成员的访问。此规则适用于方法(您不能覆盖publicmethod 并使其成为private),但您可以在此处看到类比。如果字段是protected,它将protected在每个子类中,无论多深。当然,您不能覆盖 Java 中的字段,只是为了给您一个概述。

回答by Yogendra Singh

Please refer this: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

请参考:http: //docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

The protectedmodifier specifies that the member can only be accessed within its own package(as with package-private) and, in addition, by a subclass of its classin another package.

所述受保护的修饰符指定了构件只能在被访问其自己的包(与包专用)和,另外,通过其类的子类中另一包。

So if your class Cshares the package with Aand B, its accessible.

因此,如果您的班级CA和共享包B,则它可以访问

回答by SavedBeau

Inheritance is transitive: if class B inherits from class A; and class C inherits from class B; then C is also a subclass / child class / descendant of class A.

继承是可传递的:如果 B 类继承自 A 类;C类继承自B类;那么C也是A类的子类/子类/后代。