public $name; public $sex; public $iq=10; const BIRTHPLACE='地球'; static public $counter=0;//静态属性,它是公共的资源和具体的实例没有关系 //构造函数 public function __construct($name,$sex){ self::$counter++; $this->name=$name; $this->sex=$sex; } protected function chew($food){ echo "{$food}已经被咀嚼完成!
"; } //抽象方法 abstract public function eat($food); static public function hello(){ echo '您好!我是来自'.self::BIRTHPLACE.'的人类
'; } } class Student extends Humanity { const BIRTHPLACE='火星'; public $studentId; public function test($subject){ echo "{$this->name}正在考{$subject}!
"; } public function eat($food){ $this->chew($food); echo "{$this->name}正在快速的吃{$food}!
"; } } class Worker extends Humanity { public function eat($food){ $this->chew($food); echo "{$this->name}正在的吃{$food}!
"; } } $hanMM=new Student('韩梅梅','女');
核心:父类定义抽象方法后,子类必须要实现它,不然会报错。参数个数要一致。只有抽象类里面才可以有抽象方法,抽象类里面不一定要有抽象方法
接口:public function aa(); public function bb(); } interface iB { public function cc(); public function dd(); } interface iC extends iA,iB { } class ClassA implements iA { public function aa(){ } public function bb(){ } } class ClassB implements iA,iB { public function aa(){ } public function bb(){ } public function cc(){ } public function dd(){ } } class ClassC implements iC { public function aa(){ } public function bb(){ } public function cc(){ } public function dd(){ } } $c=new ClassC();
核心在于:实现implements,也就是说定义什么接口,implements这个解决你要实现哪一个类的接口