Home > Backend Development > PHP Tutorial > PHP的抽象类的一段简单代码示例

PHP的抽象类的一段简单代码示例

WBOY
Release: 2016-06-20 12:50:40
Original
935 people have browsed it

<?php//******使用关键字abstract声明该类为抽象类***** abstract class Person{     protected $name;     protected $age;     function __construct($name="",$age=18){                 $this->name=$name;                 $this->age=$age;                 }            //***声明抽象类中的抽象方法****      //***抽象方法不必声明具体的行为***     abstract function getname();     abstract function getage();         function greeting(){         echo "hello,world";         } }  //***抽象类不能被直接实例化,只能通过子类继承***  class newPerson1 extends Person{  //***子类必须对抽象类中的全部抽象方法进行实现处理***     function getname(){             echo "the name is ".$this->name."<br/>";             }     function getage(){             echo  "the age is ".$this->age."<br/>";               }   }$tom = new newPerson1("tom",20);$tom->getname();$tom->getage();$tom->greeting();?>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template