由于只有狗可以玩"获取",这个例子是好还是坏?由于使用了instanceof,我怀疑这是一个非常糟糕的主意,但我不完全确定原因.
class Animal {
    var $name;
    function __construct($name) {
        $this->name = $name;
    }
}
class Dog extends Animal {
    function speak() {
        return "Woof, woof!";
    }
    function playFetch() {
        return 'getting the stick';
    }
}
class Cat extends Animal {
    function speak() {
        return "Meow...";
    }
}
$animals = array(new Dog('Skip'), new Cat('Snowball'));
foreach($animals as $animal) {
    print $animal->name . " says: " . $animal->speak() . '<br>';
    if ($animal instanceof Dog) echo $animal->playFetch();
}
另一个例子.由于我不断创建具有ID的数据对象,我想我也可以从基类扩展它们以避免代码重复.再次,这是不对的?由于椅子没有名字,狗没有轮子.但它们都是数据对象,因此非常令人困惑.
class Data_Object {
    protected $_id;
    function setId($id) {
        $this->_id = $id;
    }
    function getId() {
        return $this->_id;
    }
}
class Dog extends Data_Object {
    protected $_name;
    function setName($name) {
        $this->_name = 
    }
    function getName() {
        return $this->_name;
    }
}
class Chair extends Data_Object {
    protected $_numberOfWheels;
    function setNumberOfWheels($number) {
        $this->_numberOfWheels = $number;
    }
    function getNumberOfWheels() {
        return $this->_numberOfWheels;
    }
}
基本上我认为我要问的是:"如果所有子类都有相同的接口,或者它们可以有不同的接口吗?"
dec*_*eze 49
在这种情况下,讨论接口很有用.
interface Talkative {
    public function speak();
}
class Dog extends Animal implements Talkative {
    public function speak() {
        return "Woof, woof!";
    }
}
任何实现Talkative接口的动物或人(或外星人)都可以在需要健谈的环境中使用:
protected function makeItSpeak(Talkative $being) {
    echo $being->speak();
}
这是一种适当使用的多态方法.只要可能,你不在乎你正在处理什么speak().
如果Dogs也可以播放,这对他们来说很好.如果你想概括一下,也可以根据界面来考虑它.也许有一天你会得到一只训练有素的猫,它也可以玩.
class Cog extends Cat implements Playfulness {
    public function playFetch() { ... }
}
这里重要的一点是,当你打电话 playFetch()给某事时,那是因为你想玩这种动物的取物.你不打电话playFetch是因为,好吧......你可以,但因为你想在这一刻玩点球.如果您不想播放提取,则不要调用它.如果你需要在某种情况下玩提取,那么你需要一些可以播放的东西.您可以通过接口声明来确保这一点.
您可以使用类继承来实现相同的功能,但它的灵活性较差.在某些情况下,尽管存在严格的层次结构,但它非常有用:
abstract class Animal { }
abstract class Pet extends Animal { }
class Dog extends Pet {
    public function playFetch() { ... }
}
class GermanShepherd extends Dog {
    public function beAwesome() { ... }
}
然后,在某些特定的上下文中,您可能不需要任何可以执行某些操作的对象(接口),但您是专门寻找一个GermanShepherd,因为只有它可以很棒:
protected function awesomeness(GermanShepherd $dog) {
    $dog->beAwesome();
}
也许在路上你会是一个新品种的GermanShepherdS中的还真棒,但extend在GermanShepherd类.他们仍然可以使用该awesomeness功能,就像接口一样.
你当然不应该做的是循环一堆随机的东西,检查它们是什么,让它们做自己的事情.在任何情况下,这都不是很明智.
Kri*_* PC 27
PHP中多态性的另一个例子
    <?php
interface Shape {
   public function getArea();
}
class Square implements Shape {
   private $width;
   private $height;
   public function __construct($width, $height) {
      $this->width = $width;
      $this->height = $height;
   }
   public function getArea(){
      return $this->width * $this->height;
   }
}
class Circle implements Shape {
   private $radius;
   public function __construct($radius) {
      $this->radius = $radius;
   }
   public function getArea(){
      return 3.14 * $this->radius * $this->radius;
   }
}
function calculateArea(Shape $shape) {
   return $shape->getArea();
}
$square = new Square(5, 5);
$circle = new Circle(7);
echo calculateArea($square), "<br/>";
echo calculateArea($circle);
?>
| 归档时间: | 
 | 
| 查看次数: | 53391 次 | 
| 最近记录: |