接口
是一种特殊的抽象类
所有成员属性必须是常量
所有方法都是抽象的
所有成员都必须是public
使用类去实现接口中全部方法
接口跟接口之间和实现类跟接口都用如下
class Demo implements Use
//使用类去实现接口
class Demo implements Use1, User2
//接口跟接口之间
类的访问类型
private
同一个类中的内部成员都是可读、写,子类里都
不可使用
protected
同一个类中的内部成员都是可读、写,子类里都可使用
public
同一个类中的内部成员都是可读、写,
外部都可以访问读、写
interface One
{
const TEST = "hello";//接口只能是常量
function fun1();
function fun2();
}
interface Two extends One
{ //接口直接相互继承,但若类继承了后面的接口2,需要把接口1中的方法也实现了
function fun3();
function fun4();
}
interface Three
{
function fun5();
}
class Demo implements One
{ //演示普通类实现接口
function fun1()
{
echo "this is fun1()
";
}
function fun2()
{
echo "this is fun2()
";
}
}
class DemoTwo implements Two
{//演示类实现 接口的相互继承关系
function fun1()
{
echo "this is DemoTwo fun1()
";
}
function fun2()
{
echo "this is DemoTwo fun2()
";
}
function fun3()
{
echo "this is DemoTwo fun3()
";
}
function fun4()
{
echo "this is DemoTwo fun4()
";
}
}
class DemoThree implements Two, Three
{//演示类实现 继承多个接口
function fun1()
{
echo "this is DemoThree fun1()
";
}
function fun2()
{
echo "this is DemoThree fun2()
";
}
function fun3()
{
echo "this is DemoThree fun3()
";
}
function fun4()
{
echo "this is DemoThree fun4()
";
}
function fun5()
{
echo "this is DemoThree fun5()
";
}
}
class Newclass
{
function fun6()
{
echo "this is Newclass fun6()
";
}
}
class DemoFour extends Newclass implements Two, Three
{//演示类实现 继承一个类再实现多个接口
function fun1()
{
echo "this is DemoThree fun1()
";
}
function fun2()
{
echo "this is DemoThree fun2()
";
}
function fun3()
{
echo "this is DemoThree fun3()
";
}
function fun4()
{
echo "this is DemoThree fun4()
";
}
function fun5()
{
echo "this is DemoThree fun5()
";
}
}
//
//实现类1 接口直接相互继承,但若类继承了后面的接口2,需要把接口1中的方法也实现了
$demo = new Demo();
echo Demo::TEST."
";
$demo->fun1();
$demo->fun2();
///
//实现类2 演示类实现 接口的相互继承关系
$demotwo = new DemoTwo();
echo Demo::TEST."
";
$demotwo->fun1();
$demotwo->fun2();
$demotwo->fun3();
$demotwo->fun4();
///
//实现类3 演示类实现 继承多个接口
$demothree = new DemoThree();
echo Demo::TEST."
";
$demothree->fun1();
$demothree->fun2();
$demothree->fun3();
$demothree->fun4();
$demothree->fun5();
///
//实现类4 演示类实现 继承一个类再实现多个接口
$demofour = new DemoFour();
echo Demo::TEST."
";
$demofour->fun1();
$demofour->fun2();
$demofour->fun3();
$demofour->fun4();
$demofour->fun5();
$demofour->fun6();