批改状态:未批改
老师批语:
构造函数的使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<?php
class demo01{
public $num;
public $price;
public function __construct($num,$price){
$this->num = $num;
$this->price = $price;
}
public function getMoney(){
return '总共费用:'. $this->num*$this->price.'元';
}
}
$obj = new demo01(2,36);
echo $obj->getMoney();
?>
</body>
</html>点击 "运行实例" 按钮查看在线实例

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号