看到Object的clone()是protected的,然后看到《java2认证考试指南》上描述:一个对象只能请求其他对象的克隆,后者的类与被克隆对象属于同一类,或是被克隆对象的子类。

example:   C-->B-->A <--D

C对象能克隆B或A对象;B对象能克隆A对象;D对象能克隆A对象

B对象不能克隆D对象

我是读了很久没有理解,最后读了一些程序才发现这样理解:主要是visibility问题。因为是protected的,那么在子类不重载,则自动继承。子类和父类在不同package时,子类实例可以调用它自己的protected foo(), 但在子类的code中调用父类实例的protected foo(),则看不见。 //子类中用父类对象反而不能访问父类中的protected变量

之所以没能理解,原因居然是国内的很多Java书籍在介绍访问权限时,一般都这样描述:

  public protected default private
同类 T T T T
同包 T T T  
子类(不同包) T T    
无继承关系不同包的类 T      

所以我们想当然的认为,B继承A,在不同包,那么在B的code中用A的实例a调用a.clone()是可以的,但是,事实上:不行(但b.clone显然可以)

这里给出《java in a nutshell》中的一段话:【让我理解了真正的protected-你不能在B中调用A的protected方法,你可以调用自己继承于A的那个protected方法】
protected access requires a little more elaboration. Suppose class A declares a protected field x and is extended by a class B, which is defined in a different package (this last point is important). Class B inherits the protected field x, and its code can access that field in the current instance of B or in any other instances of B that the code can refer to. This does not mean, however, that the code of class B can start reading the protected fields of arbitrary instances of A! If an object is an instance of A but is not an instance of B, its fields are obviously not inherited by B, and the code of class B cannot read them.
如果一个对象o是Object实例,但不是B的实例,在B的code中不能看到o.clone,就因为它是protected字段或方法(因此需要public)
因此上面表格第三行显然没有细化protected,它说可以访问应该是指可以使用super.funProtected()或自己实例的funProtected()

代码如下:

package test;

public class Tree{
protected int age; void sayTree(){
Maple mm=new Maple();
mm.clone();//compile ok, mm is instance of Maple, also instance of Tree
}
} class Maple extends Tree{
public void sayMaple(){
Maple m=new Maple();
m.age=0; Tree t=new Tree();
t.age=0; //ok, same pack can access protected field m.clone();//compile ok, can access Maple's protected fun
t.clone();//error, t is instance of Tree, but not instance of Maple!-----------见上文黑色粗体字
}
} package test.a; import test.*;
class Pine extends Tree{
public void say(){
Pine p=new Pine();
p.age=0;
p.clone(); Tree t=new Tree();
t.age=0; // invisible
t.clone(); // invisible
}
}

从clone()谈protected的更多相关文章

  1. java Clone之深浅拷贝

    要点: 1.浅度拷贝可以不实现Cloneable接口(自动使用Object.clone)或者不重写Cloneable的clone方法. 2.要被深度拷贝的类必须实现Cloneable接口并重写clon ...

  2. 关于Clone 的方法使用

    package cn.hncu.day7.clone.v1;//克隆的套路:// 第1步:重写User类的clone()方法,以供外面调用.因为外面的类无法直接调用User类父类中的clone()方法 ...

  3. private是自己私有的,protected是可以让孩子知道的,public是公开的

    三种访问权限 public:可以被任意实体访问,数据成员和函数成员可在成员函数,友元,继承类中直接使用.亦可以作为接口,供类的用户使用 protected:只允许子类及本类的成员函数访问,在基类中用法 ...

  4. Java中protected方法访问权限的问题

    先看Test.java 此时出现上文提到的错误:The method clone from the type Object is not visiuable. 我们已经清楚Object.clone() ...

  5. Java clone方法的使用

    浅克隆 Person p2 = (Person) p1.clone(); clone()方法使用后得到p2,p2和p1指向不同的地址.但是如果p1中的属性是引用类型,那么不再对这个引用类型进行复制,而 ...

  6. protected访问权限

    Java中protected方法访问权限的问题 protected 修饰的成员变量或方法,只能在同包或子类可访问; package 1 public class TestPackage { prote ...

  7. 空间数据索引RTree完全解析及Java实现

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/MongChia1993/article/details/69941783 第一部分 空间数据的背景介 ...

  8. 空间数据索引RTree(R树)完全解析及Java实现

    第一部分 空间数据的背景介绍 空间数据的建模 基于实体的模型(基于对象)Entity-based models (or object based) 常用的空间数据查询方式 空间数据获取的方法 R树 简 ...

  9. 深入理解C++对象模型

    C++对象模型是比较重要的一个知识点,学习C++对象的内存模型,就可以明白C++中的多态原理.类的初始化顺序问题.类的大小问题等. 1 C++对象模型基础 1.1 C++对象中都有哪些东东 C++对象 ...

随机推荐

  1. Fortify规则与CERT JAVA 安全编程规范的对照表

    Fortify规则与CERT JAVA 安全编程规范的对照表http://www.automationqa.com/forum.php?mod=viewthread&tid=4353& ...

  2. ubuntu nexus 安装

    今天公司组织学习使用linux系统搭建nexus maven私服中央仓库,在公司使用centos搭建了一个,回家又用ubuntu搭建一个,主要是为了能熟悉整个流程,现将主要过程总结如下:(PS:楼主是 ...

  3. LogstashL reference 重要章节

    配置文件结构说明 https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html Event相关配 ...

  4. deepin linux安装与配置

    作者:相思羽  出处:http://www.cnblogs.com/xiang-siyu 欢迎转载,也请保留这段声明.谢谢! deepin linux是由深度开发的操作系统,基于debian,内置了搜 ...

  5. Python 基础【第八篇】变量

    1.变量定义: 给数据进行命名,数据的名字就叫做变量 2.变量格式: [变量名] = [值] 注:变量名命名需要满足下面两条准则 准则一:标示符开头不能为数字.不能包含空格.特殊字符准则二:标示符不能 ...

  6. Eclipse中的TreeViewer类和ListViewer类

    TreeViewer和TableViewer在使用上还是有很多相似之处.TreeViewer中冶有TableViewer中的过滤器和排序器.具体使用看TableViewer中的使用. 和Table有J ...

  7. Maven初学之经验浅谈

    关于Maven这个东西,我不做任何评价. 直接上资料,适合初学者 推荐资料 http://www.imooc.com/learn/443 www.imooc.com/article/15037 还有个 ...

  8. dede常用命令

    获取日期:全局:{dede:field.pubdate function="MyDate('Y-m-d H:i',@me)"/}   局部:[field:pubdate funct ...

  9. RDLC打印或导出Word的 分页设置 页边距和页面大小

    RDLC 导出Word的时候发现,Word的尺寸和页边距有问题,查了MSDN看到这样一段话 Page Sizing When the report is rendered, the Word page ...

  10. iOS夯实:ARC时代的内存管理

    iOS夯实:ARC时代的内存管理 文章转自 ARC时代的内存管理 什么是ARC Automatic Reference Counting (ARC) is a compiler feature tha ...