无性繁殖(clone)
第1题:
写clone()方法时,通常都有一行代码,是什么?
在实际编程过程中,我们常常要遇到这种情况:有一个对象A,在某一时刻A中已经包含了一些有效值,此时可能会需要一个和A完全相同新对象B,并且此后对B任何改动都不会影响到A中的值,也就是说,A与B是两个独立的对象,但B的初始值是由A对象确定的。Clone 有缺省行为,super.clone();他负责产生正确大小的空间,并逐位复制。使用clone()来复制一个对象,clone()从Object类继承。所有具有clone功能的类都有一个特性,那就是它直接或间接地实现了Cloneable接口。
protected native Object clone() throws CloneNotSupportedException;
可以看出它是一个protected方法,所以我们不能简单地调用它;关键字native,表明这个方法使用java以外的语言实现。
对于object x,
x.clone() != x
x.clone().getClass() == x.getClass()
x.clone().equals(x)
以上返回的值都为true
要说明的有两点:一是拷贝对象返回的是一个新对象,而不是一个引用。二是拷贝对象与用new操作符返回的新对象的区别就是这个拷贝已经包含了一些原来对象的信息,而不是对象的初始信息。
1.浅复制与深复制概念
⑴浅复制(浅克隆):被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。
⑵深复制(深克隆)
被复制对象的所有变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换言之,深复制把要复制的对象所引用的对象都复制了一遍。
public class TestClone1implements Cloneable{
int count;
TestClone1 next;
public TestClone1(int count) {
this.count=count;
if(count>0)
next=new TestClone1(count-1);
}
void add(){
count++;
if(next!=null)
next.count++;
}
public String toString(){
String s=String.valueOf(count)+" ";
if(next!=null)
s+=next.toString();
return s;
}
public Object clone(){
Object o=null;
try{
o=super.clone();//如果没有实现cloneable,将会抛出CloneNotSupported异常
}
catch(CloneNotSupportedException e){
System.err.println("cannot clone");
}
return o;
}
public static void main(String[] args){
TestClone1 t=new TestClone1(1);
System.out.println("t="+t);
TestClone1 t1=(TestClone1)t.clone();
System.out.println("t1="+t1);
t.add();
System.out.println("after added/nt t="+t+"/nt1="+t1)
}
}
在这个例子中创建t相当于两个相连的TestClone1实例,而在调用了t的add方法之后,意想不到的结果出现了:
t=1 0
t1=1 0
after added
t t=2 1
t1=1 1
t1也发生了改变。实际上Object.clone()进行的复制有着"bitwise"原则,也就是逐位复制。对于一个对象中定义的对象,它只是简单的复制这个对象的引用。这也就是常说的浅层拷贝(shallow copy)。想要执行深层拷贝(deep copy),只需要在TestClone1 t1=(TestClone1)t.clone();后面加上t1.next=(TestClone1)t.next.clone();就能得到:
t=1 0
t1=1 0
after added
t t=2 1
t1=1 0
这个正确的结果。
第2题:
第3题:
Datatable对象的clone是复制表的结构和数据。()
第4题:
无性繁殖(clone)
第5题:
无性繁殖系(Clone)
第6题:
Object类中的()方法不能被覆写。
第7题:
HashSet子类依靠()方法区分重复元素。
第8题:
下列代码正确的是哪项?()
第9题:
克隆意为无性繁殖,克隆技术即无性繁殖技术。
第10题:
对
错
第11题:
第12题:
第13题:
Clone 和Duplicate 之间的区别是()
A. Clone 复制出来的对象完全相同,位置也相同,Duplicate 复制出的对象位置有一点错开
B. Clone 复制出的对象完全相同,位置有点错开,Duplicate 复制出来的对象位置也相同
C. Clone 可以重复粘贴,其他都相同
D. Duplicate 可以重复粘贴,其他都相同
第14题:
以下对CoreLDRAW的Clone命令描述不正确的是()。
第15题:
Clone(克隆)和Duplicate(重制)之间的区别是什么()
第16题:
克隆是英文clone的音译,其动词含义是()。
第17题:
克隆的英文写法是什么?()
第18题:
如果一个对象仅仅声明实现了cloneable接口,但是不声明clone方法,外部能够调用其clone方法吗?()
第19题:
Clone animal:克隆动物
第20题:
Assume that you would like to clone an existing WebLogic Domain and enable some customizations. What scenario would you choose?()
第21题:
第22题:
In the Enterprise Manager, find the domain to be cloned. Choose Clone WebLogic Domain from the context menu. In the graphical wizard, customize and extend the domain if needed. These steps will only clone only the domain configuration. Binaries with deployments are needed to be cloned by operation on the file system.
In the Enterprise Manager, find the domain to be cloned. Choose Clone WebLogic Domain from the context menu. In the graphical wizard, customize the domain. These steps clone the binaries and domain configuration. If the extension is needed, perform it after cloning in the WebLogic web-based console.
In the Enterprise Manager, find the domain to be cloned. Choose Clone WebLogic Domain from the context menu. In the graphical wizard, customize and extend the domain if it is needed. These steps clone the binaries and domain configuration.
In the Enterprise Manager, find the domain to be cloned. Choose Clone WebLogic Domain from the context menu. These steps clone the binaries and domain configuration. If the customization or extension is needed, complete that after cloning in the WebLogic web-based console.
In the file system, copy the domain structure of the configuration directory and paste it in the new location. Modify configuration files for address and port. If further customization is needed, open the WebLogic web-based console and perform these modifications.
第23题:
基因复制
动物复制
有性繁殖
无性繁殖
第24题:
能
不能
不确定