public class Test3
{
public static void main(String[] args)
{
X x=null;
pass(x);
if(x!=null)
System.out.println(x.str);
}
static void pass(X x)
{
X x1=new X("hello");
try{
x=(X)x1.clone();
}
catch(Exception e)
{}
}
}
class X implements Cloneable{
String str;
X(){}
X(String str)
{
this.str=str;
}
protected Object clone() throws CloneNotSupportedException
{
super.clone();
return this;
}
}
////
我以为它会打印出"hello“,谁知道它什么也没打印出来,那clone还有什么用?
{
public static void main(String[] args)
{
X x=null;
pass(x);
if(x!=null)
System.out.println(x.str);
}
static void pass(X x)
{
X x1=new X("hello");
try{
x=(X)x1.clone();
}
catch(Exception e)
{}
}
}
class X implements Cloneable{
String str;
X(){}
X(String str)
{
this.str=str;
}
protected Object clone() throws CloneNotSupportedException
{
super.clone();
return this;
}
}
////
我以为它会打印出"hello“,谁知道它什么也没打印出来,那clone还有什么用?
解决方案 »
- 数据结构问题求解
- java 6.0 不支持在静态内部类运行main方法么?(thinking in java 的一道题在java6.0下能编译不能运行)
- jtable数据库参数传递问题
- java调用另一个软件的菜单命令。 高手请进
- 为什么我安装了VS2005后,我的JDBC不能正常连接到数据库了?
- 请问各位老大有没有方法能够找出多个实现同一个接口的类!急
- 向个位高手请教一个很简单的问题, String a=null; out.print("hello"+a+"love"); 能输出什么结果,为什么?小弟感激不进!
- 下面程序有什么问题?
- 大家看看这个程序是什么意思啊
- import时,有时候可以java.math.*有时候必须java.math.XXX这是为什么?
- 设备不可用
- 求资料
String str; XYZ() {
} XYZ(String str) {
this.str = str;
} protected Object clone() throws CloneNotSupportedException {
super.clone();
return this;
} public static void main(String[] args) {
XYZ x = null;
pass(x);
if (x != null) {
System.out.println(x.str);
} else {
System.out.println("x is null");
}
XYZ x2 = new XYZ("hi");
try {
x = (XYZ) x2.clone();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(x.str);
} static void pass(XYZ x) {
XYZ x1 = new XYZ("hello");
try {
x = (XYZ) x1.clone();
System.out.println(x.str);
} catch (Exception e) {
e.printStackTrace();
}
}
}输出
hello
x is null
hi解析,你传入pass的X 是NULL的,所以不是引用,即传入地址。所以即使CLONE成功,你原先的X还是一个NULL。呵呵,只要你将X x=null; 改成X x= new X("aa");就会出现不同的结果,你自己试一下。
如果改成X x= new X();就又会是另外一种结果,自己看看。