<?php
header(' content:text/html;charset="utf-8" ');
error_reporting(0);
abstract class Animal{ //抽象类
public $name = 'anikin';
abstract function cry(); //抽象方法,也可以没有抽象类方法;
public function getName(){ //可以实现的方法
return $this->name;
}
}
class pig extends Animal{
function cry(){
echo 'this is abstract function';
}
}
$pig1 = new pig();
$pig1->cry(); echo'<br/>';
echo $pig1->getName();
?>
<?php
header('content:text/html; charset="UTF-8" ');
error_reporting(0);
interface iUsb1{
public function a();
}
interface iUsb2{
public function b();
}
interface iUsb extends iUsb1,iUsb2{
const A=12;
public function test();
}
class bbb implements iUsb{
public function a(){ };
public function b(){ };
public function test(){ };
}
echo iUsb::A;
?>
<?php
header('content:text/html; charset="UTF-8" ');
error_reporting(0);
interface iUsb{
public function start(); //接口中的方法不能有方法体
public function stop();
const AA='90'; //定义属性必须是常量
}
class Camera implements iUsb{
public function start(){
echo '相机开始工作了';
}
public function stop(){
echo '相机停止了工作';
}
}
class Phone implements iUsb{
public function start(){
echo '手机开始工作了';
}
public function stop(){
echo '手机停止了工作';
}
}
//实例化相机
$c1 = new Camera();
$c1->start();
$c1->stop(); echo'<br/>';
//创建一个手机
$c2 = new Phone();
$c2->start();
$c2->stop();
echo iUsb::AA;
?>
<?php
header('content:text/html; charset="UTF-8" ');
error_reporting(0);
//实现接口就是对单一继承的弥补
class monkey{
public $name;
public $age;
public function climbTree(){
echo '猴子会爬树';
}
}
interface iBird{
public function fly();
}
interface iFish{
public function swimming();
}
class Little extends monkey implements iBird,iFish{
public function fly(){
echo '猴子会飞翔';
}
public function swimming(){
echo '猴子会游泳';
}
}
$L1 = new Little();
$L1->climbTree();
$L1->fly();
$L1->swimming();
?>
<?php
header(' content-type:text/html; charset="UTF-8" ');
error_reporting(0);
class A{
final public function getRate($s){
return $s*0.08;
}
}
class B extends A{
// 不能覆盖父类的方法
// public function getRate($s){
// return $s*0.01;
}
}
$b = new B();
echo $b->getRate(100);
?>
<?php
header(' content-type:text/html; charset="UTF-8" ');
error_reporting(0);
class A{
const TAX_RARE = 0.08; //在定义的时候必须给初值,前面不能使用修饰符号修饰
public function payTax($s){
// A::TAX_RARE =0.01;
return $s*A::TAX_RARE; // $s*self::TAX_RARE; self也是可以的
}
}
$a=new A();
echo $a->payTax(200);
?>