super.clone()调用的是哪个对象?
来源:互联网 发布:数据来源英文 编辑:程序博客网 时间:2024/04/27 20:29
/**
* 测试自定义clone()方法
* @author
* wavesun(wavesun@live.cn)
*/
public class Test implements Cloneable
{
private int x;
private int y;
public Test(int x,int y){
this.x=x;
this.y=y;
}
public void setLocation(int x,int y){
this.x=x;
this.y=y;
}
public Test clone(){
Test l=null;
try{
System.out.println(super.toString());
l=(Test)super.clone();
}catch(CloneNotSupportedException e){
throw new RuntimeException("not implements Cloneable");
}finally{
return l;
}
}
public String toString(){
return ""+x+","+y;
}
public static void main(String args[]){
Test lOne=new Test(5,6);
Test lTwo=lOne.clone();
lOne.x=7;
lOne.y=9;
//可以看到clone()方法正确执行
System.out.println("("+lOne.toString()+"),("+lTwo.toString()+")");
}
* 测试自定义clone()方法
* @author
* wavesun(wavesun@live.cn)
*/
public class Test implements Cloneable
{
private int x;
private int y;
public Test(int x,int y){
this.x=x;
this.y=y;
}
public void setLocation(int x,int y){
this.x=x;
this.y=y;
}
public Test clone(){
Test l=null;
try{
System.out.println(super.toString());
l=(Test)super.clone();
}catch(CloneNotSupportedException e){
throw new RuntimeException("not implements Cloneable");
}finally{
return l;
}
}
public String toString(){
return ""+x+","+y;
}
public static void main(String args[]){
Test lOne=new Test(5,6);
Test lTwo=lOne.clone();
lOne.x=7;
lOne.y=9;
//可以看到clone()方法正确执行
System.out.println("("+lOne.toString()+"),("+lTwo.toString()+")");
}
}
输出结果是inner.Test@1e57e8f
(7,9),(5,6)
super.clone是克隆当前对象,调用父类方法实现浅克隆。由于返回值是object类,所以需要强制转换类型 l=(Test)super.clone();
另外实现克隆必须实现cloneable接口。
转载自:https://zhidao.baidu.com/question/937779224323079812.html
0 0
- super.clone()调用的是哪个对象?
- super.clone的作用
- java里的clone方法,super.clone()到底调用的是谁??
- Java克隆调用super.clone()原理
- super.clone
- 下面的函数哪个是系统调用而不是库函数()?
- ListView的getFirstVisiblePosition等方法返回的是哪个对象
- Dom节点是js中的哪个对象的实例
- Java锁 到底锁的是哪个对象?
- Java锁 到底锁的是哪个对象?
- Java锁 到底锁的是哪个对象?
- 为什么调用Object的clone方法却可以使得自己类中的对象被clone呢?
- 对象的克隆(clone)
- 对象的克隆(clone)
- 对象的clone()
- 对象的克隆clone
- 对象的clone
- 对象的深clone
- spring可以识别注解
- 测试用例
- HTMlL <meta>标签总结
- "科林明伦杯"哈尔滨理工大学第六届程序设计团队赛(12.10)
- 摸爬滚打DirectX11_day03——如何获取每秒帧数(FPS)
- super.clone()调用的是哪个对象?
- 第3.1.4章 WEB系统最佳实践 js控件之bootstrap-suggest-plugin
- 个人银行账户管理程序1.0
- spring ioc使用示例
- VideoCapture_read.py cap.read() 返回一个布尔值(True/False)
- 头文件
- window10 安装完后 几个小优化
- 第十五周OJ——字符串逆序输出
- linux shell 和linux 命令的区别?windows shell 和 windows 命令呢?