class MyObject {} public class Test { public static void main(String[] args) { MyObject obj = new MyObject(); obj.clone(); // Compile error. }}
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. }}
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
|
|
|
|