美文网首页
程序组第二次作业代码

程序组第二次作业代码

作者: 爆浆芝士 | 来源:发表于2017-10-16 22:25 被阅读0次

    继承与多态:

    
    abstract class Animal
    {
        abstract public function roar();
    
        public function speak(){
            echo "I can speak!<br/>";
        }
    }
    
    class Pig extends Animal
    {
        public function roar()
        {
            echo "I want to roar as honk!<br/>";// TODO: Implement roar() method.
        }
    
        public function speak()
        {
            parent::speak();
            echo "I can speak honk!<br/>";// TODO: Change the autogenerated stub
        }
    }
    
    class Dog extends Animal
    {
        public function roar()
        {
           echo "I want to roar as 汪!<br/>";// TODO: Implement roar() method.
        }
    
        public function speak()
        {
            parent::speak();
            echo "I can speak as 汪!<br/>";// TODO: Change the autogenerated stub
        }
    }
    
    class Monkey extends Animal
    {
        public function roar()
        {
            echo "I can roar as 嗷呜——<br/>";// TODO: Implement roar() method.
        }
    
        public function speak()
        {
            parent::speak();
            echo "I can speak as 嗷呜——<br/>";// TODO: Change the autogenerated stub
        }
    }
    
    function test(Animal $animal){
        $animal->roar();
        $animal->speak();
    }
    
    test(new Pig());
    
    test(new Dog());
    
    test(new Monkey());
    /**
     * Created by PhpStorm.
     * User: HP
     * Date: 2017/10/12
     * Time: 14:49
    

    接口作业:

    
    interface iTalkable
    {
        public function talk();
    }
    
    interface iPlayable
    {
        public function play();
    }
    
    abstract class People implements iTalkable,iPlayable{
        public function talk()
        {
            echo "Would you like talk with me?<br/>";// TODO: Implement talk() method.
        }
        public function play()
        {
            echo "Would you like play with me?<br/>";// TODO: Implement play() method.
        }
    }
    
    class Teacher extends People{
        public function talk()
        {
            parent::talk();
            echo "Yes,I do.<br/>";// TODO: Change the autogenerated stub
        }
    
        public function play()
        {
            parent::play();
            echo "Yes,I'd like to!<br>";// TODO: Change the autogenerated stub
        }
    }
    
    $a = new Teacher;
    
    var_dump($a instanceof Teacher);
    echo "<br/>";
    
    function test(People $people){
        $people->play();
        $people->talk();
    }
    
    test(new Teacher());
    /**
     * Created by PhpStorm.
     * User: HP
     * Date: 2017/10/12
     * Time: 16:11
     */```
    
    
    trait作业:
    ```<?php
    
    trait MyTrait{
        public function justTalk(){
            echo "speak from ".__CLASS__."<br/>";
    
        }
    }
    
    class MyClass1{
        public function speak(){
            echo "speak from myclass1" . PHP_EOL;
        }
        use MyTrait;
    }
    class MyClass2{
        public function speak(){
            echo "speak from myclass2" . PHP_EOL;
        }
        use MyTrait;
    }
    
    $myClass1 = new MyClass1();
    $myClass1->justTalk();
    //output : speak from myClass1
    
    $myClass2 = new MyClass2();
    $myClass2->justTalk();
    //output : speak from myClass2
    /**
     * Created by PhpStorm.
     * User: HP
     * Date: 2017/10/12
     * Time: 16:52
     */```
    
    
    原地旋转暴风哭泣!!!飓风哭泣!!!

    相关文章

      网友评论

          本文标题:程序组第二次作业代码

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