作者htl74421 (<><鱼仔<>< 薰~)
看板PHP
标题[请益] class继承问题
时间Thu Jul 11 16:00:05 2013
我目前的疑惑是这样:
class grandfather { //祖父
public function grandfather()
{
//
}
public function get_data()
{
echo 'hello';
}
}
class father extends grandfather { //爸爸
public function father()
{
//
}
}
class child extends father{ //小孩
public function child()
{
//
}
public function test()
{
parent::get_data();
}
}
$a = new child();
$a->test();
我在 child 的 test() 中呼叫 father 的 get_data(), 但 father 没有,所以我想应该
会跳错误出来,可是它的结果却是再往上去找到 grandfather 的 get_data(),所以是采用
无限上纲的方式? 往上一直找到有为止?
这个样子的话那如果中间是继承了好几层然後又有人去覆写,这样不就会造成我想的
get_data()不见得一定就是我想的那个,可能中间有人做了其它修改,这样不就会大乱了吗?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 202.39.133.25
1F:→ microtech:先理解"继承"是什麽意思,哪来的无限上纲@@ 07/11 16:09
2F:→ microtech:调用函式前指名正确的类别不就不会有问题了? 怎麽乱法? 07/11 16:10
3F:→ dlikeayu:你每一层没去return parent::function() 就只会找被 07/11 16:21
4F:→ dlikeayu:覆盖的 07/11 16:22
5F:→ tkdmaf:说往上找是不对的。简单来说……这叫遗传........ 07/11 23:27
6F:推 see7di:很简单啊,用final来修饰就好了,这样就不会被重写了 07/15 21:35