java中的深拷贝与浅拷贝问题
答案:1 悬赏:50 手机版
解决时间 2021-11-12 08:06
- 提问者网友:疯子也有疯子的情调
- 2021-11-11 12:57
java中的深拷贝与浅拷贝问题
最佳答案
- 五星知识达人网友:你可爱的野爹
- 2021-11-11 14:16
浅拷贝:仅复制引用,即两个引用指向同一个对象,如:
String aString = "hello";
String bString = aString;
深拷贝:复制对象的内容,Java里通过实现Clonnable并覆盖clone方法实现,具体深度由clone的实现决定,如:
public class People implements Clonnable{
private int age;
private String name;
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void setName(String name){
this.name = name;
}
public String getAge(){
return this.name;
}
public Object clone(){
People people = new People();
people.setAge(this.age);
people.setName(this.name);
return people;
}
}
String aString = "hello";
String bString = aString;
深拷贝:复制对象的内容,Java里通过实现Clonnable并覆盖clone方法实现,具体深度由clone的实现决定,如:
public class People implements Clonnable{
private int age;
private String name;
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void setName(String name){
this.name = name;
}
public String getAge(){
return this.name;
}
public Object clone(){
People people = new People();
people.setAge(this.age);
people.setName(this.name);
return people;
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯