Java疑惑点解析(二)

用过C++的人都知道,C++中有个"拷贝构造函数"的概念。这个概念是为了解决C++中把一个对象指针赋值给另外一个对象指针,从而两个指针指向同一块内存区域而提出的。

同样,Java做为一门高级语言,它也无法避免这样的问题。Java中没有"拷贝构造函数"的概念,而是提出了一个"Clone"的概念。其实现机制还是利用C++中的"深拷贝"进行的。

下面是两个例子程序,对比一下前后就很容易得出结论了。

使用Clone机制前:

/*
* Main.java
*
* Created on 2007年8月4日, 下午6:34
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package testjavaclone;

/**
*
* @author df.sun
*/
public class Main {
private String name;
/** Creates a new instance of Main */
public Main() {
}

void setName(String name)
{
this.name = name;
}

String getName()
{
return this.name;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Main a = new Main();
Main b = a;

a.setName("aaa");
b.setName("bbb");

System.out.println(a.getName());
System.out.println(b.getName());
}

}

使用Clone机制后:

/*
* Main.java
*
* Created on 2007年8月4日, 下午6:34
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package testjavaclone;

/**
*
* @author df.sun
*/
public class Main implements Cloneable{
private String name;
/** Creates a new instance of Main */
public Main() {
}

void setName(String name)
{
this.name = name;
}

String getName()
{
return this.name;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
// TODO code application logic here
Main a = new Main();
Main b = (Main)a.clone();

a.setName("aaa");
b.setName("bbb");

System.out.println(a.getName());
System.out.println(b.getName());
}

}

关于线程共享数据的问题。

程序1:

/*
* Main.java
*
* Created on 2007年8月4日, 下午7:00
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package testthread;

/**
*
* @author df.sun
*/
public class Main extends Thread{

private int couter = 10;
/** Creates a new instance of Main */
public Main() {
}

public void run()
{
for(int i = 0;i < 10;i++)
{
couter--;
}
System.out.println(couter);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Thread a = new Thread(new Main());
Thread b = new Thread(new Main());

a.start();
b.start();
}

}

程序2:

/*
* Main.java
*
* Created on 2007年8月4日, 下午7:00
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package testthread;

/**
*
* @author df.sun
*/
public class Main extends Thread{

private int couter = 10;
/** Creates a new instance of Main */
public Main() {
}

public void run()
{
for(int i = 0;i < 10;i++)
{
couter--;
}
System.out.println(couter);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Main m = new Main();
Thread a = new Thread(m);
Thread b = new Thread(m);

a.start();
b.start();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值