欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > php >内容正文

php

php抽象的案例,php抽象类和抽象方法的例子

发布时间:2024/8/1 php 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 php抽象的案例,php抽象类和抽象方法的例子 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

/**

* php抽象类与抽象方法的例子

* edit: www.jbxue.com

*/

abstract class Animal{

function __construct($name='No-name', $breed='unknown', $price = 15) {

$this->name = $name;

$this->breed = $breed;

$this->price = $price;

}

function setName($name) {

$this->name = $name;

}

function setBreed($breed){

$this->breed = $breed;

}

function setPrice($price) {

$this->price = $price < 0 ? 0 : $price;

}

function getName() {

return $this->name;

}

function display() {

printf("

%s is a %s and costs \$%.2f.

\n", $this->name, $this->breed, $this->price);

}

public static $type = "animal";

public static function fly($direction = 'around') {

printf("

Flying %s.

\n", $direction);

}

abstract public function birdCall();

}

class Parrot extends Animal {

public function birdCall($singing=FALSE) {

$sound = $singing ? "twitter" : "chirp";

printf("

%s says: *%s*

\n", $this->getName(), $sound);

}

}

?>

总结

以上是生活随笔为你收集整理的php抽象的案例,php抽象类和抽象方法的例子的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。