首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >封装问题

封装问题
EN

Stack Overflow用户
提问于 2014-02-07 21:06:26
回答 1查看 65关注 0票数 0

我有个小问题。

我有三门课:

代码语言:javascript
复制
Class Animal {
    public $dog;
    function __construct() {
        $this->dog = new Dog();
    }
}
Class Dog {
    public $scream;
    function __construct() {
        $this->scream = new Scream();
    }
}
Class Scream {
    public $scream;
    function __construct() {
    }
    public haaa(){
       return 'hello World';
    }
}

我想得到haaa()函数..。使用

代码语言:javascript
复制
 $animal = new Animal();
 $animal->haaa();

如果函数haaa()Dog类中。效果很好..。我们有可能有深度封装的限制吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-07 21:08:53

根据您的例子,如下所示:

$animal->dog->haaa();

但是,最好将设计更改为使Dog extends Animal

代码语言:javascript
复制
class Dog extends Animal {
  public function scream(){
    // do stuff
  }
}

$dog = new Dog();
$dog->scream();

这更有语义,因为狗属于动物的“王国”。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21637756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档