PHP OOP – 抽象类
PHP - 什么是抽象类和方法?
抽象类和方法是指父类有一个命名方法,但需要其子类来完成任务。
抽象类是包含至少一个抽象方法的类。 抽象方法是已声明但未在代码中实现的方法。
一个抽象类或方法是用 abstract
关键字定义的:
语法
<?php
abstract class ParentClass {
abstract public function someMethod1();
abstract public function someMethod2($name, $color);
abstract public function someMethod3() : string;
}
?>
abstract class ParentClass {
abstract public function someMethod1();
abstract public function someMethod2($name, $color);
abstract public function someMethod3() : string;
}
?>
从抽象类继承时,子类方法必须定义为相同的名称,以及相同或更少限制的访问修饰符。 因此,如果抽象方法被定义为受保护,则子类方法必须被定义为受保护或公共,但不是私有的。 此外,所需参数的类型和数量必须相同。 但是,子类可能还有可选参数。
所以,当子类继承自抽象类时,我们有以下规则:
- 子类方法必须定义同名并重新声明父抽象方法
- 子类方法必须使用相同或更少限制的访问修饰符定义
- 所需参数的数量必须相同。 但是,子类可能还有可选参数
我们来看一个例子:
实例
<?php
// Parent class
abstract class Car {
public $name;
public function __construct($name) {
$this->name = $name;
}
abstract public function intro() : string;
}
// Child classes
class Audi extends Car {
public function intro() : string {
return "Choose German quality! I'm an $this->name!";
}
}
class Volvo extends Car {
public function intro() : string {
return "Proud to be Swedish! I'm a $this->name!";
}
}
class Citroen extends Car {
public function intro() : string {
return "French extravagance! I'm a $this->name!";
}
}
// Create objects from the child classes
$audi = new audi("Audi");
echo $audi->intro();
echo "<br>";
$volvo = new volvo("Volvo");
echo $volvo->intro();
echo "<br>";
$citroen = new citroen("Citroen");
echo $citroen->intro();
?>
亲自试一试 »
// Parent class
abstract class Car {
public $name;
public function __construct($name) {
$this->name = $name;
}
abstract public function intro() : string;
}
// Child classes
class Audi extends Car {
public function intro() : string {
return "Choose German quality! I'm an $this->name!";
}
}
class Volvo extends Car {
public function intro() : string {
return "Proud to be Swedish! I'm a $this->name!";
}
}
class Citroen extends Car {
public function intro() : string {
return "French extravagance! I'm a $this->name!";
}
}
// Create objects from the child classes
$audi = new audi("Audi");
echo $audi->intro();
echo "<br>";
$volvo = new volvo("Volvo");
echo $volvo->intro();
echo "<br>";
$citroen = new citroen("Citroen");
echo $citroen->intro();
?>
实例解析
Audi、Volvo 和 Citroen 类继承自 Car 类。 这意味着,由于继承,Audi、Volvo 和 Citroen 类可以使用公共 $name 属性以及 Car 类的公共 __construct() 方法。
但是,intro() 是一个抽象方法,应该在所有子类中定义,并且它们应该返回一个字符串。
PHP - 更多抽象类示例
让我们看另一个抽象方法有参数的例子:
实例
<?php
abstract class ParentClass {
// Abstract method with an argument
abstract protected function prefixName($name);
}
class ChildClass extends ParentClass {
public function prefixName($name) {
if ($name == "John Doe") {
$prefix = "Mr.";
} elseif ($name == "Jane Doe") {
$prefix = "Mrs.";
} else {
$prefix = "";
}
return "{$prefix} {$name}";
}
}
$class = new ChildClass;
echo $class->prefixName("John Doe");
echo "<br>";
echo $class->prefixName("Jane Doe");
?>
亲自试一试 »
abstract class ParentClass {
// Abstract method with an argument
abstract protected function prefixName($name);
}
class ChildClass extends ParentClass {
public function prefixName($name) {
if ($name == "John Doe") {
$prefix = "Mr.";
} elseif ($name == "Jane Doe") {
$prefix = "Mrs.";
} else {
$prefix = "";
}
return "{$prefix} {$name}";
}
}
$class = new ChildClass;
echo $class->prefixName("John Doe");
echo "<br>";
echo $class->prefixName("Jane Doe");
?>
让我们看另一个例子,抽象方法有一个参数,子类有两个可选参数,父类的抽象方法中没有定义:
实例
<?php
abstract class ParentClass {
// Abstract method with an argument
abstract protected function prefixName($name);
}
class ChildClass extends ParentClass {
// The child class may define optional arguments that are not in the parent's abstract method
public function prefixName($name, $separator = ".", $greet = "Dear") {
if ($name == "John Doe") {
$prefix = "Mr";
} elseif ($name == "Jane Doe") {
$prefix = "Mrs";
} else {
$prefix = "";
}
return "{$greet} {$prefix}{$separator} {$name}";
}
}
$class = new ChildClass;
echo $class->prefixName("John Doe");
echo "<br>";
echo $class->prefixName("Jane Doe");
?>
亲自试一试 »
abstract class ParentClass {
// Abstract method with an argument
abstract protected function prefixName($name);
}
class ChildClass extends ParentClass {
// The child class may define optional arguments that are not in the parent's abstract method
public function prefixName($name, $separator = ".", $greet = "Dear") {
if ($name == "John Doe") {
$prefix = "Mr";
} elseif ($name == "Jane Doe") {
$prefix = "Mrs";
} else {
$prefix = "";
}
return "{$greet} {$prefix}{$separator} {$name}";
}
}
$class = new ChildClass;
echo $class->prefixName("John Doe");
echo "<br>";
echo $class->prefixName("Jane Doe");
?>