Java中受保护的访问修饰符的含义 [英] The meaning of protected access modifier in Java

查看:94
本文介绍了Java中受保护的访问修饰符的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我的问题是:
我的项目有2个软件包,分别名为package1(内部包含文件Circle.java和Test.java),package2(内部包含文件Test2.java). Test2类是Circle类的子类,而Circle具有受保护的字段名称z.情况是这样的:

像这样的Circle类:

Hello everyone!

My question is that:
My project has 2 packages named package1 (with file Circle.java and Test.java inside), package2 (with file Test2.java inside). Class Test2 is subclass of class Circle and Circle has a protected field name z. The situation is like this:

class Circle like this:

package package1;

public class Circle {
	private int x;
	private int y;
	protected int z;
	public Circle()
	{}
}



Test2类是这样的:



Class Test2 is like this:

package package2;
import package1.Circle;

public class Test2 extends Circle{
	private Circle c = new Circle(); 
	void modifyCircleObjectState()
	{
		c.z = 20;
	}
}



Eclipse说:字段Circle.z不可见.
跟随链接 http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol .html [ ^ ]
我发现,protected修饰符指定该成员只能在其自己的程序包中访问(与package-private一样),此外,该成员可以在另一个程序包中访问其类的子类.

好的,Test2是Circle的子类,它位于另一个包(在本例中为package2)中.那么,为什么从Test2中看不到对象c的z字段.

我的想法出了什么问题?



Eclipse says: The field Circle.z is not visible.
Following the link http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html[^]
I''ve found that 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.

Alright, Test2 is a subclass of Circle and it''s located in another package (in this case package2). So why from Test2 the z field of object c is not visible.

What is wrong with my thinking?

Any help or suggesstion would be appreciated!

推荐答案

好吧,您在这里扩展了Circle,因此您可以直接访问Test2内部的z.您不能做的是实例化Circle(在这里您已将其作为c完成),然后尝试访问protected字段.如果您像这样更改代码
Well, you are extending Circle here, so you have direct access to z inside Test2. What you can''t do is instantiate Circle (here you''ve done it as c), and try to access a protected field. If you change your code like this
package package2;
import package1.Circle;
public class Test2 extends Circle {
  void modifyCircleObjectState()
  {
    z = 20;
  }
}

如您所见,没有单独的Circle实例-因此,当有人实例化Test2并调用modifyCircleObjectState时,z在那里被更新.

As you can see, there is no separate Circle instance - so when somebody instantiates Test2 and calls modifyCircleObjectState, z is updated there.


您可以访问父类的z,但不能访问Circle实例的z.意味着您只能访问继承的z,而不能访问任何其他Circle类的z值.
比较一下,就像是关于自己的孩子一样.您可以访问他们的名字值并将其设置为所需的任何名称,但是即使他们是成年人的后代,也无法确定其他人的孩子的名字.他们根本不属于你.这可能会引起一个问题,为什么adopt没有可能性?在C ++中,您可以声明被授予更多访问权限的友元运算符,但是java中没有等效的运算符.

祝你好运!
You can access the z of your super class but not access z of an instance of Circle. Meaning you can only access the inherited z, but not the z value of any other Circle class.
Compare it like if it was about your own kids. You can access their name value and set it to any name you want, but you can not decide the name of the kids of other people, even though they are descendant of an adult. They simply do not belong to you. This might raise the question why there is no possibility to adopt? In C++ you can declare friend operators which are granted more access but there is no equivalent in java.

Good luck!


这可以阐明上面的解决方案2. protected 的意思是,从具有受保护变量的类继承的类可以访问该变量(对于在父类中声明的私有变量而言,这不是正确的).其他类无法访问受保护的变量.
This may clarify solution 2 above. What protected means is that classes that inherit from a class with a protected variable can access that variable (which would not be true for a private variable declared in the parent class). Other classes do not have access to the protected variable.


这篇关于Java中受保护的访问修饰符的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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