Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
class stuInfo{ //class关键在定义一个类
public $name; //类属性; public代表访问控制为公开
public $age;
public $height;
protected $weight; //protected :受保护的,可以被继承,不能被外部调用
private $bonus; //private : 私用属性,只能再当前类中用
public function demo() //类方法
{
//$this:代表当前类
return $this->name. '的身高是'.$this->height;
}
public function demo1()
{
return $this->name.'的年龄是'.$this->age.'岁';
}
}
$stuInfo = new stuInfo;//类的是实例化就是对象
echo $stuInfo->name = '胡六','<br>';
echo $stuInfo->age = 25,'<br>';
echo $stuInfo->height = '172cm','<br>';
echo $stuInfo->demo(),'<br>';
echo $stuInfo->demo1();
<?php
class BBA
{
public $name;
public $color;
protected $displacement;
private $price;
//__construct系统函数来定义一个类方法,当类被实例化的时候,会先调用这个方法
public function __construct($name,$color,$displacement,$price)
{
//构造方法就是给类的每个属性赋值,完成对象的初始化
$this->name = $name;
$this->color = $color;
$this->displacement = $displacement;
$this->price = $price;
}
}
//有构造方法的类,再创造对象的时候必须做属性的赋值,来完成对象的初始化
$BWM = new BBA('330li运动曜夜版','skyblue','2.0T','RMB380000元');
echo $BWM->name;
$Audi = new BBA('Audi A4 运动版','white','2.0T','RMB350000元');
echo $Audi->color;
$BENS = new BBA('BENS C260 豪华型', 'balck','1.8T','RMB300000元');
echo $BENS->name;