关于clone的一个小问题(急)

时间:2021-11-20 14:31:48
package test;
import java.util.Date;
public class tr implements Cloneable{
private int id;
private double area;
private Date whenBuilt;
public tr(int id,double area){
this.id=id;
this.area=area;
whenBuilt=new Date();
}
public Object clone(){
try{
return super.clone();
}
catch(CloneNotSupportedException ex){
return null;
}
}
public static void main(String[] args){
Object a=new tr(1,2);
a.clone();
}
}
在这个程序中,a.clone()他的错误是the method clone() from the type Object is not visible ,既然我已经重载了Object中的clone()方法,a.clone()应该调用的是tr类中的clone()方法,为什么还有以上的报错?

11 个解决方案

#1


Object中clone()方法是被声明为protected;protected被修饰的方法只能在本类,同一个包中的类,它的子类中使用;出现上面的问题我认为就是tr类不属于该几种情况;所以就出现这样的问题;the method clone() from the type Object is not visible;
clone()方法使用时应注意的问题是:
 x.clone() != x

  x.clone().getClass() == x.getClass()

  x.clone().equals(x)
 Object的clone的行为是最简单的。以堆上的内存存储解释的话(不计内务内存),对一个对象a的clone就是在堆上分配一个和a在堆上所占存储空间一样大的一块地方,然后把a的堆上内存的内容复制到这个新分配的内存空间上。

#2


上面的代码应该为下面:
package test;
import java.util.Date;
public class tr implements Cloneable{
private int id;
private double area;
private Date whenBuilt;
public tr(int id,double area){
this.id=id;
this.area=area;
whenBuilt=new Date();
}
public Object clone(){
try{
return super.clone();
}
catch(CloneNotSupportedException ex){
return null;
}
}
public static void main(String[] args){
Object a=new tr(1,2);
 tr b=(tr)a.clone();//因为a.clone()返回的是一个Object类型的;
}
}

#3


抱歉上面的代码应该为下面:
package test;
import java.util.Date;
public class tr implements Cloneable{
private int id;
private double area;
private Date whenBuilt;
public tr(int id,double area){
this.id=id;
this.area=area;
whenBuilt=new Date();
}
public tr clone(){
try{
return (tr)super.clone();
}
catch(CloneNotSupportedException ex){
return null;
}
}
public static void main(String[] args){
 tr a=new tr(1,2);
 tr b=a.clone();//因为a.clone()返回的是一个Object类型的;
}
}

#4


可是Object类是所有类的父类,那突然类肯定可以调用Object中的clone()啊?怎么会不可见?

#5


public class tr implements Cloneable {
private int id;
private double area;
private Date whenBuilt;

public tr(int id, double area) {
this.id = id;
this.area = area;
whenBuilt = new Date();
}

public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException ex) {
return null;
}
}

public static void main(String[] args) {
tr a = new tr(1, 2);   //用这个就行了
                //Object a=new tr(1,2);  //你怎么总喜欢用Object型的,此时你这个a是Object
                                         //型的,调用的不是你重写的clone方法
                                               //是Object的clone方法,而pretected型的不能在
                                               //子类中实例化后调用,所以不对
a.clone();
}
}

#6


这种错误完全可以避免的,想建哪个类的实例最好就用哪个类型的,
tr a = new tr(1, 2);   
new tr(1, 2);  就用tr型
别乱用Object型
还有类名首字母要大写 

#7


 


package test;
public class Text extends Text2{
public void printList(){
System.out.println("234");
}
public static void main(String[] args) {
Text2 text = new Text();
text.printList();
}

}

package test;
public class Text2 {
private int i=0;
public Text2(){
i=2;
}
protected void printList(){
System.out.println("123");
}
}

输出地结果是234,而非123,由此可见,调用的是子类中覆盖掉的printLset()方法,并不是父类中的,所以我就有疑问为什么clone()已经在子类中覆盖了却还是会调用父类中的,还有楼上说的protected方法不能在子类中实例化后调用我没法理解,能否举个小例子说明

#8


LZ这不是重载,是重写吧。但是重写的话 好像不可以把修饰符权限放大……现在在网吧,不能试验 LZ试下吧

不知道LZ重写clone方法的目的……

#9


方法复制要求所有属性对应对象也可以复制

#10


学习了!!!

#11


Object 的 clone 方法是 protected,所以没办法使用,而在 tr 中可以使用时因为你提升了clone 的范围,但是你如果把 tr 转换为 Object 那么除非你再转换回来,否则是不能访问的。

#1


Object中clone()方法是被声明为protected;protected被修饰的方法只能在本类,同一个包中的类,它的子类中使用;出现上面的问题我认为就是tr类不属于该几种情况;所以就出现这样的问题;the method clone() from the type Object is not visible;
clone()方法使用时应注意的问题是:
 x.clone() != x

  x.clone().getClass() == x.getClass()

  x.clone().equals(x)
 Object的clone的行为是最简单的。以堆上的内存存储解释的话(不计内务内存),对一个对象a的clone就是在堆上分配一个和a在堆上所占存储空间一样大的一块地方,然后把a的堆上内存的内容复制到这个新分配的内存空间上。

#2


上面的代码应该为下面:
package test;
import java.util.Date;
public class tr implements Cloneable{
private int id;
private double area;
private Date whenBuilt;
public tr(int id,double area){
this.id=id;
this.area=area;
whenBuilt=new Date();
}
public Object clone(){
try{
return super.clone();
}
catch(CloneNotSupportedException ex){
return null;
}
}
public static void main(String[] args){
Object a=new tr(1,2);
 tr b=(tr)a.clone();//因为a.clone()返回的是一个Object类型的;
}
}

#3


抱歉上面的代码应该为下面:
package test;
import java.util.Date;
public class tr implements Cloneable{
private int id;
private double area;
private Date whenBuilt;
public tr(int id,double area){
this.id=id;
this.area=area;
whenBuilt=new Date();
}
public tr clone(){
try{
return (tr)super.clone();
}
catch(CloneNotSupportedException ex){
return null;
}
}
public static void main(String[] args){
 tr a=new tr(1,2);
 tr b=a.clone();//因为a.clone()返回的是一个Object类型的;
}
}

#4


可是Object类是所有类的父类,那突然类肯定可以调用Object中的clone()啊?怎么会不可见?

#5


public class tr implements Cloneable {
private int id;
private double area;
private Date whenBuilt;

public tr(int id, double area) {
this.id = id;
this.area = area;
whenBuilt = new Date();
}

public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException ex) {
return null;
}
}

public static void main(String[] args) {
tr a = new tr(1, 2);   //用这个就行了
                //Object a=new tr(1,2);  //你怎么总喜欢用Object型的,此时你这个a是Object
                                         //型的,调用的不是你重写的clone方法
                                               //是Object的clone方法,而pretected型的不能在
                                               //子类中实例化后调用,所以不对
a.clone();
}
}

#6


这种错误完全可以避免的,想建哪个类的实例最好就用哪个类型的,
tr a = new tr(1, 2);   
new tr(1, 2);  就用tr型
别乱用Object型
还有类名首字母要大写 

#7


 


package test;
public class Text extends Text2{
public void printList(){
System.out.println("234");
}
public static void main(String[] args) {
Text2 text = new Text();
text.printList();
}

}

package test;
public class Text2 {
private int i=0;
public Text2(){
i=2;
}
protected void printList(){
System.out.println("123");
}
}

输出地结果是234,而非123,由此可见,调用的是子类中覆盖掉的printLset()方法,并不是父类中的,所以我就有疑问为什么clone()已经在子类中覆盖了却还是会调用父类中的,还有楼上说的protected方法不能在子类中实例化后调用我没法理解,能否举个小例子说明

#8


LZ这不是重载,是重写吧。但是重写的话 好像不可以把修饰符权限放大……现在在网吧,不能试验 LZ试下吧

不知道LZ重写clone方法的目的……

#9


方法复制要求所有属性对应对象也可以复制

#10


学习了!!!

#11


Object 的 clone 方法是 protected,所以没办法使用,而在 tr 中可以使用时因为你提升了clone 的范围,但是你如果把 tr 转换为 Object 那么除非你再转换回来,否则是不能访问的。