PHP 抽象类
抽象类是接口和类的混合体。它可以定义功能以及接口。
- 扩展抽象类的类必须实现抽象类中定义的所有抽象方法。
- 抽象类的声明方式与普通类相同,只是添加了’abstract’关键字。
语法
abstract class MyAbstract
{
//Methods
}
//And is attached to a class using the extends keyword.
class Myclass extends MyAbstract
{
//class methods
}
示例1
<?php
abstract class a
{
abstract public function dis1();
abstract public function dis2();
}
class b extends a
{
public function dis1()
{
echo "javatpoint";
}
public function dis2()
{
echo "SSSIT";
}
}
obj = new b();obj->dis1();
$obj->dis2();
?>
输出:
示例2
<?php
abstract class Animal
{
public name;
publicage;
public function Describe()
{
return this->name . ", " .this->age . " years old";
}
abstract public function Greet();
}
class Dog extends Animal
{
public function Greet()
{
return "Woof!";
}
public function Describe()
{
return parent::Describe() . ", and I'm a dog!";
}
}
animal = new Dog();animal->name = "Bob";
animal->age = 7;
echoanimal->Describe();
echo $animal->Greet();
?>
输出: