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();
}

}



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1726408


优质内容筛选与推荐>>
1、动手动脑4
2、第四、渐变和图案
3、淘宝开源的PB级分布式数据库系统OceanBase简介
4、POJ 1279 Art Gallery (半平面交)
5、最大相同子串


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号