本文概述
抽象类是接口和类之间的混合体。它可以定义功能以及接口。
- 扩展抽象类的类必须实现抽象类中定义的所有抽象方法。
- 抽象类的声明方式与类相同, 但附加了’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 "srcmini";
}
public function dis2()
{
echo "SSSIT";
}
}
$obj = new b();
$obj->dis1();
$obj->dis2();
?>
输出
例子2
<?php
abstract class Animal
{
public $name;
public $age;
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;
echo $animal->Describe();
echo $animal->Greet();
?>
输出
来源:
https://www.srcmini02.com/29270.html