`
阿尔萨斯
  • 浏览: 4169212 次
社区版块
存档分类
最新评论

Java疑惑点解析(二)

 
阅读更多
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>

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

}




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics