class
MyObject {}
public
class Test {
public static void main(String[] args) {
MyObject obj = new MyObject();
obj.clone(); // Compile error.
}
}
|
class
MyObject2 {
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public
class Test2 {
public static void main(String[] args) throws CloneNotSupportedException {
MyObject2 obj = new MyObject2();
obj.clone(); // Compile OK.
}
}
|
package
1
class
MyObject3 {
protected
Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package
2
public
class Test3 extends MyObject3 {
public static void main(String args[]) {
MyObject3 obj = new MyObject3();
obj.clone(); // Compile error.
Test3 tobj = new Test3();
tobj.clone();// Complie OK.
}
}
|
|
public
|
protected
|
default
|
private
|
同类
|
T
|
T
|
T
|
T
|
同包
|
T
|
T
|
T
|
|
子类(不同包)
|
T
|
T
|
|
|
不同包中无继承关系的类
|
T
|
|
|
|
本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/19287
总结:protected
1.在同包中能被访问
2.同包,直接子类中能被访问
3.同类能被访问
必须在同包中能被访问