您的位置:首页 > 编程语言 > Java开发

你应该知道的10件关于Java 6的事情

2008-01-03 10:20 826 查看
        用过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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: