1

次の構造を持つ ListNode を構築しました。

 class MyNode {
    private  $weight;
    private  $children;
    private  $t1;
    private  $t2;
    private  $t3;
    *** 
    more variables
    ***
    function __constructr($weight, $t1, $t2, $t3, $children = array()) {
        $this->weight = $weight;
        $this->children = $children;
        $this->t1 = $t1;
        $this->t2 = $t2;
        $this->t3 = $t3;
    }

ここで、データは同じで重みが異なる 5 つのノードを作成します。

    $n1 = new MyNode(25, 't_1', 't_2', 't_3');
    $n2 = new MyNode(30, 't_1', 't_2', 't_3');
    $n3 = new MyNode(49, 't_1', 't_2', 't_3');
    $n4 = new MyNode(16, 't_1', 't_2', 't_3');
    $n5 = new MyNode(62, 't_1', 't_2', 't_3');

t1、t2、および t3 は異なる場合がありますが、この 5 つのノードでは同じであることに注意してください。上記を行う代わりに、ある種のクローン機能を使用して次のことを行いたい

    $n1 = new MyNode(25, 't_1', 't_2', 't_3');
    $n2 = $n1->clone(array('weight' => 30));
    $n3 = $n2->clone(array('weight' => 49));
    $n4 = $n4->clone(array('weight' => 16));
    $n5 = $n5->clone(array('weight' => 62));

clone 関数は、変更したい MyNode 内の変数名とその値であるキーの配列を取ります。したがって、array('weight' => 30)は$this->weight = 30; に変更する必要があります。 配列から変数にアクセスするのに行き詰まりました。すべての値が現在のノードと同じである新しいノードを作成する必要がありますが、配列にあるもののみを変更する必要があります。

   function clone($changeVariables) {
      -----
   }
4

3 に答える 3

1

これを試して:

$obj = clone $this;

foreach ($changeVariables as $field => $val) {
    $obj->{$field} = $val;
}
return $obj;
于 2013-03-21T14:20:35.570 に答える
1

観察

  • clone予約語と呼ばれるメソッドまたは関数を実装することはできません
  • それはphpでオブジェクトを複製する方法ではありません
  • __constructr間違っていてconstruct、php で設定する有効な方法ではありません

必要なものは次のとおりです。

class MyNode {
    private $weight;
    private $children;
    private $t1;
    private $t2;
    private $t3;

    function __construct($weight, $t1, $t2, $t3, $children = array()) {
        $this->weight = $weight;
        $this->children = $children;
        $this->t1 = $t1;
        $this->t2 = $t2;
        $this->t3 = $t3;
    }

    public function getClone(array $arg) {
        $t = clone $this;
        foreach ( $arg as $k => $v ) {
            $t->{$k} = $v;
        }
        return $t;
    }
}

$n1 = new MyNode(25, 't_1', 't_2', 't_3');
$n2 = $n1->getClone(array(
        'weight' => 30
));

print_r($n1);
print_r($n2);

出力

MyNode Object
(
    [weight:MyNode:private] => 25
    [children:MyNode:private] => Array
        (
        )

    [t1:MyNode:private] => t_1
    [t2:MyNode:private] => t_2
    [t3:MyNode:private] => t_3
)
MyNode Object
(
    [weight:MyNode:private] => 30
    [children:MyNode:private] => Array
        (
        )

    [t1:MyNode:private] => t_1
    [t2:MyNode:private] => t_2
    [t3:MyNode:private] => t_3
)
于 2013-03-21T14:25:27.733 に答える
0

変数変数は1つの解決策です。

foreach ($changeVariables as $key => $value) {
    $this->{$key} = $value;
}

$this->{$key}設定を許可する前に、存在するかどうかを確認することで拡張できます。

http://php.net/manual/en/language.oop5.cloning.php

全体的な結果は次のようになります。

function clone($changeVariables) {
    $newObj = clone $this;
    foreach ($changeVariables as $key => $value) {
        $newObj->{$key} = $value;
    }
    return $newObj;
}
于 2013-03-21T14:19:07.450 に答える