标签:echo print 构造 private sel 自动生成 UNC sch end
<?php
class Person{
public $name;
public $age;
public static $ss = "第一个";
function __construct($name,$age){
// TODO:实现构造函数()方法
$this->name=$name;
$this->age=$age;
}
function say(){ // 公有
echo "名字:" . $this->name . "\n";
echo "年龄:" . $this->age . "\n";
echo "性别:" . $this->xingB() . "\n";
echo "学校:" . $this->school() . "\n";
echo self::$ss . "\n";
}
private function xingB(){ // 私有
return "男生";
}
protected function school(){ // 受保护
return "xu";
}
protected function play(){
echo "12345 ";
}
function __destruct()
{
// TODO: Implement __destruct() method.
// TODO:实现析构函数()方法
echo "父类 再见" . $this->name . "\n";
}
}
class sonRen extends Person{
function saySon(){
echo "名字是" . $this->name . "\n";
echo "年龄是" . $this->age . "\n";
echo "性别是" . $this->xingB() . "\n";
echo "学校是" . $this->school() . "\n";
}
public function xingB(){ // 子类私有方法在这里无法重写
$nan = "女生";
return $nan;
}
public function school(){ // 子类可以更改父类的受保护方法
return "pt";
}
public function play()
{
parent::play(); // TODO: Change the autogenerated stub
// TODO:更改自动生成的存根
echo "6789\n";
}
function __destruct()
{
// TODO: Implement __destruct() method.
// TODO:实现析构函数()方法
echo "子类 拜拜" . $this->name . "\n";
}
}
$r = new Person("san",18); // 父类访问父类方法
print_r($r->say() . "\n");
$s = new sonRen("wu",20); // 子类访问子类方法
print_r($s->saySon() . "\n");
print_r($s->play() . "\n"); // 子类访问父类
print_r($s->say(). "\n"); // 子类访问父类方法
标签:echo print 构造 private sel 自动生成 UNC sch end
原文地址:https://www.cnblogs.com/see-you-cu/p/12069478.html