如果您引用它的表达式的编译时类型是您自己的类或子类,则只能访问不同包中类型的受保护成员. (其中“你的”类是包含代码的类.)你自己的类必须是原来声明方法的类的子类.

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

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 {}

换句话说,它允许您访问“像你一样的对象”的受保护成员.

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 Access to a protected Member

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保_java.lang.Object的受保护方法如何保护子类?相关推荐

  1. java object大小_在Java中,确定对象object大小的最佳方法是什么? - Break易站

    例如,假设我有一个应用程序,它可以读取包含一堆数据行的CSV文件.我根据数据类型向用户提供行数摘要,但是我想确保不读取太多数据行并导致OutOfMemoryErrors.每一行都会翻译成一个对象.有没 ...

  2. java 字符串函数_Java字符串函数– 25+必须知道方法

    java 字符串函数 Java字符串函数 (Java String Functions) Java String class has a lot of functions to manipulate ...

  3. java 数组效率_java数组复制的四种方法效率对比

    有关数组的基础知识,有很多方面,比方说初始化,引用,遍历,以及一维数组和二维数组,今天我们先看看数组复制的有关内容. 来源于牛客网的一道选择题: JAVA语言的下面几种数组复制方法中,哪个效率最高? ...

  4. java泛型方法作用_java泛型的作用与使用方法是什么?

    泛型,即"参数化类型".一提到参数,最熟悉的就是定义方法时有形参,然后调用此方法时传递实参.那么参数化类型怎么理解呢?顾名思义,就是将类型由原来的具体的类型参数化,类似于方法中的变 ...

  5. java 生成随机数_Java 生成随机数的 N 种方法

    原标题:Java 生成随机数的 N 种方法 www.baeldung.com/java-generating-random-numbers 1.引言 本文将探讨用 Java 生成随机数的不同方法. 2 ...

  6. java private 接口_java接口中 定义 private 私有方法

    在传统的Java编程中,被广为人知的一个知识点是:java Interface接口中不能定义private私有方法.只允许我们定义public访问权限的方法.抽象方法或静态方法.但是从Java 9 开 ...

  7. JAVA进阶教学之(Object类的toString方法)

    1.toString public String toString() 返回对象的字符串表示形式.总的来说,这 toString方法返回一个字符串,"以文本方式表示"这个对象.其结 ...

  8. java 数组删除_Java数组删除指定元素的方法(按数组角标删除)

    Java最有效的删除数组指定元素的方法,应该属于这一种形式,下面是Java数组按角标删除数据的工具类,代码如下:public static byte[] removeTheElement(byte[] ...

  9. java lang保_java中lang包下的类都涉及哪几方面的

    展开全部 JDK API文档 ,   你可以下载一个 , 可以非常方便的查看类库软件包 java.lang  :  提供利用 Java 编程语言进行程序设计的基础类.32313133353236313 ...

最新文章

  1. VS Code插件之Cordova Tools
  2. UITableView中cell的圆角(第一个和最后一个)
  3. syntax error: unexpected end of file
  4. SAP应用搜索分页的实现原理
  5. 输入框不可以输入中文
  6. [转载] python sorted 使用cmp函数时候注意cmp需要传入两个参数,传入两个参数机制的分析
  7. 大规模电机控制的方案选择-电机和驱动器篇
  8. 华三路由交换配置命令_H3C路由器常用基本配置命令
  9. 【软件工具】--- 软件安装管家目录
  10. 圆弧周长公式_弧长计算公式
  11. 优启通做服务器系统,系统安装教程1:制作优启通PE启动盘
  12. linux环境启动tomcat成功后,访问链接一直在转圈
  13. 动手学Docker-第二弹-基本操作
  14. 庖丁解牛山linux内核的析,# 庖丁解牛Linux内核分析笔记-1
  15. npm私服发包及使用
  16. matlab 产生每次都一样的随机数
  17. 关于oracle怎么保证读一致性
  18. 小程序富文本图片放大功能
  19. Android中的 eBPF 流量监控
  20. 企业项目实战k8s篇(十三)k8s容器资源限制

热门文章

  1. 大剑无锋之素数【面试推荐】
  2. vb for循环 combobox的使用 Print的使用
  3. NumPy之:标量scalars
  4. Leet Code OJ 136. Single Number [Difficulty: Medium]
  5. python表格对齐_Python对Excel表格数据重新排版
  6. 深度学习与自然语言处理之四:卷积神经网络模型(CNN)
  7. Hard Disk Drive HDU - 4788——24行代码AC,解题报告
  8. 15行代码AC——Link/Cut Tree CodeForces - 614A(爆long long处理+快速幂讲解)
  9. [leetcode]541.反转字符串||
  10. (*长期更新)软考网络工程师学习笔记——Section 11 Linux操作系统简介