美文网首页
#新人作业task3

#新人作业task3

作者: Bronze_Yi | 来源:发表于2017-10-12 19:29 被阅读0次

    作业1

    这里定义了一个抽象类Animal

    <?php
    abstract class Animal{
        public abstract function  roar() ;
        public function speak( $what )
        {
            echo $what."<br/>" ;
        }
    }
    

    接下来是三个类的定义

    <?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!") ;
        }
    }
    

    接下来定义一个测试函数,并进行测试。

    <? php
    function test()
    {
        $myDog = new Dog() ;
        $myMonkey = new Monkey() ;
        $myPig = new Pig() ;
        $myDog ->roar() ;
        $myMonkey ->roar() ;
        $myPig ->roar() ;
    }
    test() ;
    

    测试结果为:
    I'm a dog!
    I'm a monkey!
    I'm a pig!

    作业2

    先定义两个接口

    <?php
    interface iTalkable
    {
        public function talk() ;
    }
    
    interface iPlayable
    {
        public function play() ;
    }
    

    再在抽象类People中实现接口中的方法:

    <?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类,并在其中进行测试

    <?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() ;
    

    结果为:
    I'm playing!
    I'm the father of Kai.Z!
    I'm talking!

    相关文章

      网友评论

          本文标题:#新人作业task3

          本文链接:https://www.haomeiwen.com/subject/bdboyxtx.html