<?php
/**
* 写一个父类一个子类,练习extends,public,private,protected的用法
*/
header('content-type:text/html; charset=utf8');
class Fruit
{
public $name;
protected $addr;
private $quality;
public function __construct($name='苹果',$addr='山西'){
$this->name = $name;
$this->addr = $addr;
}
public function getAddr($addr='山西'){
return $this->name.'产地是'.$addr;
}
}
class Bananer extends Fruit
{
public $color;
public $weight;
public function __construct($name='香蕉',$color='黄色的',$weight='5kg'){
parent::__construct($name);
$this->color = $color;
$this->weight = $weight;
}
public function getInfo(){
return $this->name.'是'.$this->color;
}
public function getWeight(){
return $this->name.'是'.$this->weight;
}
// public function getAddr(){
// return $this->addr;
// } 不能再写这个
}
echo '1.'.(new Fruit())->getAddr();
echo '<br>';
echo '2.'.(new Bananer())->getInfo();
echo '<br>';
echo '3.'.(new Fruit('橘子'))->getAddr('湖南');
echo '<br>';
echo '4.'.(new Bananer('香蕉','','100kg'))->getWeight();
echo '<br>';
echo '5.父类中的变量$addr'.(new Bananer())->getAddr();
echo '<br>';
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!