Blogger Information
Blog 42
fans 4
comment 0
visits 30551
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5.4 父类、子类及方法重载 --31Day
小丑的博客
Original
894 people have browsed it

父类

实例

<?php


class Area {
    
//    长
    protected $legthNum = 0;
//    宽
    protected $widthNum = 0;
    
//    常量 π
    const pai = 3.14;
    
//   构造方法
    public function __construct($lengthNum=0,$widthNum=0)
    {
       $this->legthNum = $lengthNum;
       $this->widthNum = $widthNum;
    }
    
//    面积
    public function ares()
    {        
    }
    
//    周长
    public function perimeter()
    {        
    }


    //put your code here
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

子类

实例

<?php

require 'Area.php';

//长方形/长方体
class Square extends Area{
    //put your code here
    
    
    //属性长方体-- 高
    
    protected $heigthNum=0;
    
    
    public function __construct($lengthNum = 0, $widthNum = 0,$heigthNum=0)
    {
        $this->legthNum=$lengthNum;
        $this->widthNum=$widthNum;
        
        $this->heigthNum=$heigthNum;
    }
    
//    面积计算 方法重载
    public function ares()
    {
        $squareAre = $this->legthNum*$this->widthNum;
        return $squareAre;
    }
//    周长计算 方法重载
    public function perimeter()
    {
        $squarePerimeter = ($this->legthNum + $this->widthNum)*2;
        return $squarePerimeter;
    }
 
    //    体积计算
    public function volume(){
        $squareVolume = $this->legthNum*$this->widthNum*$this->heigthNum;
        return $squareVolume;
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

页面引用

实例

<?php


spl_autoload_register(function($class){
    
    require 'class/'.$class.'.php';
});

$length = 5;
$width = 6;
$height = 10;
$sq = new Square($length,$width,$height);

echo '面积:'.$sq->ares().'<br>';

echo '周长:'.$sq->perimeter().'<br>';

echo '体积:'.$sq->volume();

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post