object cloning

php.net

<?php
class SubObject
{
    static $instances = 0;
    public $instance;

    public function __construct() {
        $this->instance = ++self::$instances;
    }

    public function __clone() {
        $this->instance = ++self::$instances;
    }
}

class MyCloneable
{
    public $object1;
    public $object2;

    function __clone()
    {
        $this->object1 = clone $this->object1;
    }
}

$obj = new MyCloneable();

$obj->object1 = new SubObject();
$obj->object2 = new SubObject();

$obj2 = clone $obj;

print(‘Original Object:‘."\n");
print_r($obj);

print(‘Cloned Object:‘."\n");
print_r($obj2);
  Original Object:
  MyCloneable Object
  (
      [object1] => SubObject Object
          (
              [instance] => 1
          )
   
      [object2] => SubObject Object
          (
              [instance] => 2
          )
   
  )
  Cloned Object:
  MyCloneable Object
  (
      [object1] => SubObject Object
          (
              [instance] => 3
          )
   
      [object2] => SubObject Object
          (
              [instance] => 2
          )
   
  )
   
时间: 2024-08-09 14:36:57

object cloning的相关文章

PBOC规范下的java卡介绍

JAVA卡与智能卡 什么是 JAVA 卡呢?JAVA 卡是一种可以运行 JAVA 程序的接触式微处理器智能卡.1996 年 11 月,JAVA 卡 1.0 版本的规范正式发布了.如今 JAVA 卡最新的规范已经到了 2.1 版. 相信对智能卡比较了解的读者一定会问:智能卡的出现和使用已经快有二十年了,为什么会在最近出现 JAVA 卡的呢?为什么 JAVA 卡会变得如此受欢迎?为了回答这个问题,我们先来回顾一下 JAVA 卡出现之前的智能卡,看看它存在着什么样的问题. JAVA卡之前的智能卡 早期

Java面向对象程序设计--接口和内部类

1.接口的定义: In the Java programming language, an interface is not a class but a set of requirements for classes that want to conform the interface. 说明: 1) Interface 不是class,虽然interface具有class的一些特点,例如能够继承,能够定义相同类型的变量,而且和C++的abstract class非常像,但Java 的inter

j详细说明ava于clone办法

原文地址:http://leihuang.org/2014/11/14/java-clone/ In java, it essentially means the ability to create an object with similar state as the original object. 什么是clone 字典中的意思就是复制(强调跟原来的一模一样). *By default, java cloning is 'field by field copy' *.由于Object类不知

详解java中clone方法

原文地址:http://leihuang.net/2014/11/14/java-clone/ In java, it essentially means the ability to create an object with similar state as the original object. 什么是clone 字典中的意思就是复制(强调跟原来的一模一样). *By default, java cloning is 'field by field copy' *.因为Object类不知

大话设计模式第九章---原型模式PHP实现

首先,PHP对象clone参考资料: http://php.net/manual/en/language.oop5.cloning.php#object.clone Object Cloning¶ Creating a copy of an object with fully replicated properties is not always the wanted behavior. A good example of the need for copy constructors, is i

Prototype Design Pattern

Before we proceed to Prototype Design Pattern, We need to review on Clone() method in Java. The Object cloning is a way to create exact copy of an object. The clone() method of Object class is used to clone an object. The java.lang.Cloneable interfac

Concurrent.Thread.js

(function(){ if ( !this.Data || (typeof this.Data != 'object' && typeof this.Data != 'function') ) this.Data = new Object(); if ( this.Data.Stack === undefined ) this.Data.Stack = undefined; with ( function(){ with ( Data ) { return function () {

java Object对象的clone方法

参考copy链接:http://blog.csdn.net/bigconvience/article/details/25025561 在看原型模式,发现要用到clone这个方法,以前和朋友聊过,没怎么看过,刚好要用,就看看了. 源码解释: /** * Creates and returns a copy of this object. The precise meaning * of "copy" may depend on the class of the object. The

Object类的源码分析

JDK 1.8中Object 的源码如下: 1 public class Object { 2 3 private static native void registerNatives(); 4 static { 5 registerNatives(); 6 } 7 8 /** 9 * Returns the runtime class of this {@code Object}. The returned 10 * {@code Class} object is the object tha