0
  1. Is there a way to get rid of the getCopy() method in the derived class HelloAndGoodbye, given that it looks the same as the getCopy() in the base class Hello?

  2. And what is more, what is there an efficient way to achieve this?

(the only difference between the two functions is that in the baseclass 'static' refers to 'Hello' and in the derived class 'static' refers to 'HelloAndGoodbye; as for the variables contained therein they can be easily renamed so that they are the same in both functions).

<?php

class Hello {

  private $hello;

  public function createStub() {

    return new static(null);

  }

  public function __construct($hello) {

    $this->setHello($hello);

  }

  public function getCopy() {

    $helloCopy = static::createStub();

    $this->doCopy($helloCopy);

    return $helloCopy;

  }

  public function doCopy($helloCopy) {

    $helloCopy->setHello($this->hello);

  }

  public function setHello($hello) {

    $this->hello = $hello;

  }

  public function getHello($hello) {

    return $this->hello;

  }

  public function __toString() {

    return $this->hello . "\n";

  }

}

class HelloAndGoodbye extends Hello {

  private $goodbye;

  public function createStub() {

    return new static(null, null);

  }

  public function __construct($hello, $goodbye) {

    parent::__construct($hello);

    $this->setGoodbye($goodbye);

  }

  public function getCopy() {

    $helloAndGoodbyeCopy = static::createStub();

    $this->doCopy($helloAndGoodbyeCopy);

    return $helloAndGoodbyeCopy;

  }

  public function doCopy($helloAndGoodbyeCopy) {

    parent::doCopy($helloAndGoodbyeCopy);

    $helloAndGoodbyeCopy->setGoodbye($this->goodbye);

  }

  public function setGoodbye($goodbye) {

    $this->goodbye = $goodbye;

  }

  public function getGoodbye($goodbye) {

    return $this->goodbye;

  }

  public function __toString() {

    return parent::__toString() . $this->goodbye . "\n";

  }

}

function test() {

  $hello = new Hello("Hello John");

  $helloAndGoodbye = new HelloAndGoodbye("Hello Jane", "Goodbye Jane");

  echo $hello;

  echo $helloAndGoodbye;

}

test();

OUTPUT:

Hello John
Hello Jane
Goodbye Jane
4

1 回答 1

0

我通过使用与__CLASS__它出现的类的名称相对应的 PHP 常量找到了手头问题的解决方案。这让我摆脱了派生类中的伪重复 getCopy() 方法,同时仍然允许 getCopy() 在两者上正常工作:

<?php

class Hello {

  private $hello;

  public function createStub() {

    return new static(null);

  }

  public function __construct($hello) {

    $this->setHello($hello);

  }

  public function getCopy() {

    $class = __CLASS;

    $instanceCopy = $class::createStub();

    $this->doCopy($instanceCopy);

    return $instanceCopy;

  }

  public function doCopy($helloCopy) {

    $helloCopy->setHello($this->hello);

  }

  public function setHello($hello) {

    $this->hello = $hello;

  }

  public function getHello($hello) {

    return $this->hello;

  }

  public function __toString() {

    return $this->hello . "\n";

  }

}

class HelloAndGoodbye extends Hello {

  private $goodbye;

  public function createStub() {

    return new static(null, null);

  }

  public function __construct($hello, $goodbye) {

    parent::__construct($hello);

    $this->setGoodbye($goodbye);

  }

  public function doCopy($helloAndGoodbyeCopy) {

    parent::doCopy($helloAndGoodbyeCopy);

    $helloAndGoodbyeCopy->setGoodbye($this->goodbye);

  }

  public function setGoodbye($goodbye) {

    $this->goodbye = $goodbye;

  }

  public function getGoodbye($goodbye) {

    return $this->goodbye;

  }

  public function __toString() {

    return parent::__toString() . $this->goodbye . "\n";

  }

}

function test() {

  $hello = new Hello("Hello John");

  $helloAndGoodbye = new HelloAndGoodbye("Hello Jane", "Goodbye Jane");

  echo $hello;

  echo $helloAndGoodbye;

}

test();

输出:

Hello John
Hello Jane
Goodbye Jane
于 2015-08-22T15:53:43.557 回答