Java疑惑点解析(二)

news/2024/7/7 11:35:14

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





http://www.niftyadmin.cn/n/3648830.html

相关文章

android开发中的权限与权限获取

访问登记属性android.permission.ACCESS_CHECKIN_PROPERTIES &#xff0c;读取或写入登记check-in数据库属性表的权限获取错略位置android.permission.ACCESS_COARSE_LOCATION&#xff0c;通过WiFi或移动基站的方式获取用户错略的经纬度信息&#xff0c;定位精度大概误差在30~1…

拦截Activity的启动流程绕过AndroidManifest检测

首先非常简单启动activity public void skip(View view){Intent intentnew Intent(this,TestActivity.class);startActivity(intent);} 这里TestActivity没有进行注册 HookStartActivityUtil工具封装类 public class HookStartActivityUtil {private String TAG "HookS…

汇编中一个有趣的问题

int main(){ int a1; int b2; int c-1;} 问题是下面哪个关系成立: &a>&b>&c还是&a<&b<&c? 我们知道局部变量是存放在栈中的,a先PUSH,然后是b,最后是c。 而栈指针SP是从高地址→低地址方向移动的,所以&a>&b>&am…

如何在Node.js中编写异步代码

The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program. 作者选择了“ 开放互联网/言论自由基金会”作为“ Write for DOnations”计划的一部分来接受捐赠。 介绍 (Introduction) For many programs in …

Android Webview遇到的问题——记新版广告墙开发

前一阵很辛劳&#xff0c;所以荒废了博客。前几天终于完成了这项艰苦卓绝的工程&#xff1a;HTML5版广告墙&#xff0c;决定写篇文章&#xff0c;记录一下踩过的坑。 项目介绍 广告墙属于典型的列表式应用&#xff1a;打开后是无尽列表&#xff0c;通过滑动手指驱使列表滚动&am…

如何在Ubuntu 20.04上安装Apache Web服务器[快速入门]

介绍 (Introduction) The Apache HTTP server is the most widely-used web server in the world. It provides many powerful features, including dynamically loadable modules, robust media support, and extensive integration with other popular software. Apache HTTP…

360 DroidPlugin——插件化架构

https://github.com/Qihoo360/DroidPlugin 打开网址&#xff0c;下载源码用AS导入lib,添加依赖继承PluginApplication&#xff0c;如果不继承可以如下 Overridepublic void onCreate() {super.onCreate();PluginHelper.getInstance().applicationOnCreate(getBaseContext()); …

Android Intent应用,打开网页,安装程序,打电话,打开地图等功能

[java] view plaincopy 1.从google搜索内容 Intent intent new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY,"searchString") startActivity(intent); 2.浏览网页 Uri uri Uri.parse("http:…