作業(yè)1
這里定義了一個(gè)抽象類Animal
<?php
abstract class Animal{
public abstract function roar() ;
public function speak( $what )
{
echo $what."<br/>" ;
}
}
接下來是三個(gè)類的定義
<?php
require_once "Animal.php" ;
class Pig extends Animal
{
public function roar()
{
$this->speak("I'm a pig!") ;
}
}
class Monkey extends Animal
{
public function roar()
{
$this->speak("I'm a monkey!") ;
}
}
class Dog extends Animal
{
public function roar()
{
$this->speak("I'm a dog!") ;
}
}
接下來定義一個(gè)測試函數(shù)雄家,并進(jìn)行測試。
<? php
function test()
{
$myDog = new Dog() ;
$myMonkey = new Monkey() ;
$myPig = new Pig() ;
$myDog ->roar() ;
$myMonkey ->roar() ;
$myPig ->roar() ;
}
test() ;
測試結(jié)果為:
I'm a dog!
I'm a monkey!
I'm a pig!
作業(yè)2
先定義兩個(gè)接口
<?php
interface iTalkable
{
public function talk() ;
}
interface iPlayable
{
public function play() ;
}
再在抽象類People中實(shí)現(xiàn)接口中的方法:
<?php
require_once "InterfaceContainer.php" ;
abstract class People implements iTalkable,iPlayable
{
public function play()
{
echo "I'm playing!"."<br/>" ;
}
public function talk( )
{
echo "I'm talking!"."<br/>" ;
}
}
最后由Teacher類繼承People類,并在其中進(jìn)行測試
<?php
require_once "People.php" ;
class Teacher extends People
{
public function Teachertalk( $what )
{
echo $what."<br/>" ;
}
// ……
}
$myTeacher = new Teacher() ;
$myTeacher->play() ;
$myTeacher->Teachertalk("I'm the father of Kai.Z!" ) ;
$myTeacher->talk() ;
結(jié)果為:
I'm playing!
I'm the father of Kai.Z!
I'm talking!