【发布时间】:2017-08-20 12:21:03
【问题描述】:
我创建了一个抽象类并声明了两个公共函数和一个抽象函数。 之后,我在抽象类中为公共函数创建了主体,但是当我将此类扩展到另一个类时,方法主体没有调用,那么我想知道公共方法与主体类的用途。
<?php
//declare abstract class
abstract class Test {
// declare method 1 with body
public function method1()
{
echo "hello"; //what is use of this code here
}
public function method2()
{
$this->method3();
}
abstract public function method3();
}
//extended abstract class to another class
class AnotherTest extends Test{
public function method1(){
echo "5+9"/2;
}
public function method3(){
echo 2+9;
}
public function method2()
{
echo "just for testing";
}
}
$obj = new AnotherTest();
$obj->method1();
echo nl2br("\n");
$obj->method3(9,10);
echo nl2br("\n");
$obj->method2();
?>
【问题讨论】:
-
好吧,如果你不重写它们,它会调用你在
Test.. 中声明的方法。但你应该多阅读一些关于 OO 的内容,以完全理解它为什么以及如何工作。
标签: php methods abstract-class abstract