欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

作业2

程序员文章站 2022-04-19 21:26:11
...
<?php
abstract class Animals
{
    public $height;
    protected $weight;
    protected $age;
    abstract function roar();
    function speak()
    {
        echo "i can speak\n";
    }
}

class Pig extends Animals
{
    function roar()
    {
        echo "hmghmg\n";
    }
    function speak()
    {
        echo "hengheng\n";
    }
}

class Dog extends Animals
{
    function roar()
    {
        echo "wangwang\n";
    }
    function speak()
    {
        echo "wawa\n";
    }
}

class Monkey extends Animals
{
    function roar()
    {
        echo "dai,chianlaosunyibang\n";
    }
    function speak()
    {
        echo "yaoguainalipao\n";
    }
}

interface iTalkable
{
    function talk();
}

interface iPlayable
{
    function play();
}

abstract class People implements iTalkable,iPlayable
{
    function play()
    {
        echo "playing is my nature\n";
    }
    abstract function talk();
}

class Teacher extends People
{
    function talk()
    {
        echo "I want to talk with students\n";
    }
    function play()
    {
        echo "I have had enough of playing\n";
    }
}

function speak(Animals $a1)
{
    $a1->speak();
}

$d1=new dog;
$m1=new monkey;

speak($d1);
speak($m1);

?>

<?php
trait T1
{
    public function talk()
    {
        echo "my class is ".__class__.PHP_EOL;
    }
}

class Father
{
    use T1;
    public function talk()
    {
        echo "I am a father and the class is ".__class__.PHP_EOL;
    }
}
class Son1 extends Father
{

}
class Son2 extends Father
{
    use T1;
}
class Son3 extends Father
{
    public function talk()
    {
        echo "I am a son and my class is ".__CLASS__.PHP_EOL;
    }
}
$s1=new Son1;
$s2=new Son2;
$s3=new Son3;
$s1->talk();
$s2->talk();
$s3->talk();
?>

“`

“`作业2