php构造方法对成员变量赋值 function__construct
我初学者有很多不懂,下面是代码。我感觉对的但是dw里说function__construct有错误。求大神帮忙看看!顺便指出下问题!/*设计学校学生教室教师*//*人数D...
我初学者有很多不懂,下面是代码。我感觉对的 但是dw里说
function__construct有错误。求大神帮忙看看!顺便指出下问题!
/*设计 学校 学生 教室 教师*/
/*人数 DZ 20 50 2 */
<?php
class School{
public $school_name;
public $school_student;
public $school_room;
public $school_teacher;
function__construct($name,$student,$room,$teacher){
$this->$school_name=$name;
$this->$school_student=$student;
$this->$school_room=$room;
$this->$school_teacher=$teacher;
}
function show(){
echo "!@#$%^&*";
}
}
class People extends School{
public $teachername;
function__construct($tname,$studentconsts){
$this->$teachername=$tname;
$this->$school_student=$studentconsts;
}
function show(){
return "今天上课".$teachername."讲课,学生".$studentconsts."人";}
}
class Tongji extends School{
function show(){
return "学校名:".$this->$school_name."学生数:".$this->$school_student."教室数:".$this->$school_room."教师数:".$this->$school_teacher;
}
}
$a=new People("周周周",20);
$b=new School("DZ",20,50,2);
echo $a->show()."<br>";
echo $b->show();
?> 展开
function__construct有错误。求大神帮忙看看!顺便指出下问题!
/*设计 学校 学生 教室 教师*/
/*人数 DZ 20 50 2 */
<?php
class School{
public $school_name;
public $school_student;
public $school_room;
public $school_teacher;
function__construct($name,$student,$room,$teacher){
$this->$school_name=$name;
$this->$school_student=$student;
$this->$school_room=$room;
$this->$school_teacher=$teacher;
}
function show(){
echo "!@#$%^&*";
}
}
class People extends School{
public $teachername;
function__construct($tname,$studentconsts){
$this->$teachername=$tname;
$this->$school_student=$studentconsts;
}
function show(){
return "今天上课".$teachername."讲课,学生".$studentconsts."人";}
}
class Tongji extends School{
function show(){
return "学校名:".$this->$school_name."学生数:".$this->$school_student."教室数:".$this->$school_room."教师数:".$this->$school_teacher;
}
}
$a=new People("周周周",20);
$b=new School("DZ",20,50,2);
echo $a->show()."<br>";
echo $b->show();
?> 展开
2个回答
展开全部
function __construct
function 是系统关键词,表示定义一个方法,后面加空格然后根方法名( __construct
是方法名)。你的未加空格
__construct 是系统内置的,叫魔术方法,每次实例化类是会自动执行此方法。
还有就是调用类的成员变量不需要在变量前面加 $ 比如:
$this->school_name = $name; 正确
$this->$school_name = $name; 错误
<?php
class School {
public $school_name;
public $school_student;
public $school_room;
public $school_teacher;
function __construct($name, $student, $room, $teacher) {
$this->school_name = $name;
$this->school_student = $student;
$this->school_room = $room;
$this->school_teacher = $teacher;
}
function show() {
echo "!@#$%^&*";
}
}
class People extends School {
public $teachername;
function __construct($tname, $studentconsts) {
$this->teachername = $tname;
$this->school_student = $studentconsts;
}
function show() {
return "今天上课" . $this->teachername . "讲课,学生" . $this->school_student . "人";
}
}
class Tongji extends School {
function show() {
return "学校名:" . $this->school_name . "学生数:" . $this->school_student . "教室数:" . $this->school_room . "教师数:" . $this->school_teacher;
}
}
$a = new People ( "周周周", 20 );
$b = new School ( "DZ", 20, 50, 2 );
echo $a->show () . "<br>";
echo $b->show ();
?>
function 是系统关键词,表示定义一个方法,后面加空格然后根方法名( __construct
是方法名)。你的未加空格
__construct 是系统内置的,叫魔术方法,每次实例化类是会自动执行此方法。
还有就是调用类的成员变量不需要在变量前面加 $ 比如:
$this->school_name = $name; 正确
$this->$school_name = $name; 错误
<?php
class School {
public $school_name;
public $school_student;
public $school_room;
public $school_teacher;
function __construct($name, $student, $room, $teacher) {
$this->school_name = $name;
$this->school_student = $student;
$this->school_room = $room;
$this->school_teacher = $teacher;
}
function show() {
echo "!@#$%^&*";
}
}
class People extends School {
public $teachername;
function __construct($tname, $studentconsts) {
$this->teachername = $tname;
$this->school_student = $studentconsts;
}
function show() {
return "今天上课" . $this->teachername . "讲课,学生" . $this->school_student . "人";
}
}
class Tongji extends School {
function show() {
return "学校名:" . $this->school_name . "学生数:" . $this->school_student . "教室数:" . $this->school_room . "教师数:" . $this->school_teacher;
}
}
$a = new People ( "周周周", 20 );
$b = new School ( "DZ", 20, 50, 2 );
echo $a->show () . "<br>";
echo $b->show ();
?>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询