美文网首页
新人作业task2

新人作业task2

作者: Bronze_Yi | 来源:发表于2017-09-28 11:02 被阅读0次

    People.php

    <?php
    namespace  MyProject ;
     class People
    {
      private $name ; // string
      private $age ; //int
      private $height ; //double
      private $weight ; // double
      private $hobby=array() ; //stringArray
      public static $currentNumber=0 ; //int
      const MAXNUMBER=100 ;
    
      public function __construct (  $name ,  $age ,  $height , $weight , array $hobby )
      {
          $this->name = $name ;
          $this->age  = $age ;
          $this->height = $height ;
          $this->weight = $weight ;
          $this->hobby = $hobby ;
          self::$currentNumber++ ;
          echo "A person has been constructed<br>" ;
          if( self::$currentNumber >= self::MAXNUMBER )
          {
            echo "There are too much person<br>" ;
            $this->__destruct() ;
          }
    
      }
      public function __destruct()
      {
        echo "A person has been destructed\n" ;
        self::$currentNumber-- ;
        // TODO: Implement __destruct() method.
      }
    
         public function speak( $what )
      {
        echo $what."<br>" ;
      }
      public function eat()
      {
        $this->speak(__METHOD__) ;
        $this->weight+=0.5 ;
      }
      public function sleep()
      {
        $this->speak(__METHOD__) ;
        $this->height+=0.1 ;
      }
    
      public function haveBirthday()
      {
        $this->speak(__METHOD__) ;
        $this->age++ ;
      }
    
      public function swim()
      {
        $this->speak(__METHOD__) ;
        $this->weight-=0.5 ;
      }
    
      public function run()
      {
         $this->speak(__METHOD__) ;
         $this->weight-=0.5 ;
      }
    
      public function walk()
      {
         $this->speak(__METHOD__) ;
      }
    }
     
     // 以下是测试运行部分: 
     $a =  new People("Chengyi", 19 , 178 , 72 , array() ) ;
     $a -> eat() ;
     $a -> sleep() ;
     $a -> haveBirthday() ;
     $a -> run() ;
     $a -> walk() ;
     $a -> speak("I finished this task!") ; 
    ?>
    

    运行结果

    A person has been constructed
    MyProject\People::eat
    MyProject\People::sleep
    MyProject\People::haveBirthday
    MyProject\People::run
    MyProject\People::walk
    I finished this task!
    A person has been destructed

    相关文章

      网友评论

          本文标题:新人作业task2

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