受保护成员的范围 [英] Scope of protected members

查看:170
本文介绍了受保护成员的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iam准备SCJP,我也知道受保护的成员范围在包内以及其他包中,只有继承可能有一些条件。

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.

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

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;
  ...............
}

背后的原因是,在Friendclass中,成员x不接受为其分配值,如果是Childclass,则表现为私有成员。

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.

推荐答案

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

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

编辑:

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

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

作为TotalFrickinRockstarFromMars的摘要状态和Java访问控制文档也说明,这意味着不允许 Friendclass 访问字段 x

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.

这篇关于受保护成员的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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