如何保护java.lang.Object的受保护方法不受子类的影响? [英] How are java.lang.Object's protected methods protected from subclasses?

查看:131
本文介绍了如何保护java.lang.Object的受保护方法不受子类的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关键字 protected 授予对同一包和子类中的类的访问权限( http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html )。

The keyword protected grants access to classes in the same package and subclasses (http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html).

现在,每个类都有 java.lang.Object 作为超类( http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object。 html )。

Now, every class has java.lang.Object as superclass (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html).

因此我得出结论,每个类都可以访问 java.lang.Object 's即使它们是受保护的方法

Hence I conclude that every class may access java.lang.Object's methods even if they are protected.

看一下以下示例:


public class Testclass {
  public Object getOne() throws CloneNotSupportedException {
    return this.clone();
  }
  public Object getTwo() throws CloneNotSupportedException {
    return ((Object) this).clone();
  }
}

虽然 getOne()编译罚款, getTwo()给予


Testclass.java:6: clone() has protected access in java.lang.Object
        return ((Object) this).clone();

我既不明白为什么 getTwo()不编译也没有什么区别(关于 java.lang.Object s成员的访问权限)与 getOne()

I neither understand why getTwo() doesn't compile nor what's the difference (regarding the access of java.lang.Objects members) with getOne().

推荐答案

如果您引用它的表达式的编译时类型是您的,则只能访问不同包中类型的受保护成员自己的类或子类。 (其中your类是包含代码的类。)您自己的类也必须是最初声明该方法的类型的子类。

You can only access protected members of a type in a different package if the compile-time type of the expression you're referencing it through is either your own class or a subclass. (Where "your" class is the class containing the code.) Your own class has to be a subclass of the type which originally declares the method, too.

这是一个例;假设 Base 与所有其他类位于不同的包中:

Here's an example; assume that Base is in a different package to all the other classes:

package first;
public class Base
{
    protected void Foo() {}
}

// Yes, each class is really in its own file normally - but treat
// all the classes below as being in package "second"

package second;
public class Child extends Base
{
    public void OtherMethod(Object x)
    {
        ((Base) x).Foo(); // Invalid: Base is not Child or subclass
        ((Child) x).Foo(); // Valid: Child is Child
        ((GrandChild) x).Foo(); // Valid: GrandChild is subclass of Child
        ((OtherChild) x).Foo(); // Invalid: OtherChild is not Child or subclass
    }
}

public class GrandChild extends Child {}
public class OtherChild extends Base {}

换句话说,它允许您访问与您类似的对象的受保护成员。

In other words, it's letting you have access to the protected members of "objects which are a like you".

详细信息在 Java语言规范的第6.6.2节


A protected
的成员或构造函数一个对象可以从外部访问
,在其中声明为
的包只能由负责
的代码执行那个对象。

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

6.6.2.1访问受保护的会员

C 是声明
受保护成员 m 的类。访问
仅允许在 C
子类 S 的正文中。此外,如果Id
表示实例字段或实例
方法,则:如果访问是通过
限定名称​​ Q.Id ,则其中 Q 是一个
ExpressionName ,当且仅当
的类型为表达式 Q 时才允许访问
S
S 的子类。如果访问是通过字段访问
表达式 E.Id ,其中 E 主要
表达式,或者通过方法调用
表达式 E.Id(...),其中 E
主要表达式,当且仅当 E
的类型是 S S 的子类时,才允许访问

Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then: If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S. If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.

这篇关于如何保护java.lang.Object的受保护方法不受子类的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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