受保护成员范围

发布于 2024-11-06 01:59:18 字数 531 浏览 4 评论 0原文

我正在为 SCJP 做准备,我还知道受保护的成员范围位于包内以及其他包中,并且具有某些条件,例如只有继承才可能。

例如 : 我有三个类作为 Parentclass Childclass Friendclass

package x.parent;

 class Parentclass{
 protected int x=10;
  ...............
  }

 package x.child;

 class Childlass extends Parentclass{
  super.x=20;
  ...............
}

 package x.child;

 import x.parent.Parentclass;

 class Friendclass{
 Parentclass pc = new Parentclass();
 pc.x=30;
  ...............
}

背后的原因是什么,在 Friendclass 中,成员 x 不会接受为其分配值,在 Childclass 的情况下表现为私有成员。

Iam preparing for SCJP , also i came to know that protected members scope is within the package as well as in other package with some conditions like possible only with inheritance.

For example :
i have three classes as Parentclass Childclass Friendclass

package x.parent;

 class Parentclass{
 protected int x=10;
  ...............
  }

 package x.child;

 class Childlass extends Parentclass{
  super.x=20;
  ...............
}

 package x.child;

 import x.parent.Parentclass;

 class Friendclass{
 Parentclass pc = new Parentclass();
 pc.x=30;
  ...............
}

Whats the reason behind that, in Friendclass the member x will not accept to assign a value to that, behaves as private member not in case of Childclass.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对岸观火 2024-11-13 01:59:18

有四个访问修饰符

private - just this class
no modifier - just this class or this package (NOT subclass)
protected - just this class, this package, or subclass
public - everyone and their cousin

由于它使用默认修饰符,因此如果满足以下条件之一,则它具有访问权限:

  1. 是类本身的一部分(不!)
  2. 是类本身的包的一部分(不!)

因此它会失败标准,因此您无权访问。

There are four access modifiers

private - just this class
no modifier - just this class or this package (NOT subclass)
protected - just this class, this package, or subclass
public - everyone and their cousin

Since it uses the default modifier, it has access if one of the following is true:

  1. Is part of the class itself (Nope!)
  2. Is part of the package of the class itself (Nope!)

So it fails the criteria, and so you don't get access.

笛声青案梦长安 2024-11-13 01:59:18

您甚至无法访问 Childclass 中的 Parentclass.x,因为 x 具有默认可见性(不受保护)。请参阅 http://download.oracle.com/javase/tutorial/java/ javaOO/accesscontrol.html

编辑:

x.child.Friendclassx.parent.Parentclass 不在同一个包中。
x.child.Friendclass 不继承x.parent.Parentclass

正如 TotalFrickinRockstarFromMars 的摘要所述和 Java 访问控制文档也指出的那样,这意味着 Friendclass 不允许访问字段 x

You can't even access Parentclass.x in Childclass because x has default visibility (not protected). See http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

edit:

x.child.Friendclass is not in the same package as x.parent.Parentclass.
x.child.Friendclass does not inherit from x.parent.Parentclass.

as TotalFrickinRockstarFromMars's summary states and the Java access control docs also state, this means that Friendclass is not allowed to access the field x.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文