-3
class Parent implements Cloneable{
int i ;
Parent(){
}
Parent(int i){
this.i = i;
}
public Object clonemyobj() throws CloneNotSupportedException{
return this.clone();
}
protected Object cloneme() throws CloneNotSupportedException {
return this.clone();
}
}
class Child extends Parent{
}
public class CloneDemo{
public static void main(String... ar) throws CloneNotSupportedException
{
Parent p = new Parent(10);
Object o1 = p.cloneme();
// 1. cloneme() is a protected method in parent class i am able to access it
//Object o2 = p.clone();
// 2. the above line shows clone() has protected access in java.lang.object
// but that should be the same with cloneme() method in parent class
// why?
Object o = new Object();
Child c = new Child();
//3. o.clone();
c.cloneme();
//4.(c.cloneme()) is a protected method in parent class
// but still able to access it using child class object
}
}
我所有的上述类都在同一个包中。受保护访问修饰符如何工作,它与访问对象类有何区别克隆方法
- 任何人都可以提供我解释所有上述四个要点吗?
- 这些都将如何执行?
[编辑]你的问题有适当的代码和文本格式。 – user1803551
修复了您的代码和文本格式 –
为什么不运行它并查看*这些每个都会如何执行?* – NewUser