亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建的類有錯誤。幫助找到它,請)

創建的類有錯誤。幫助找到它,請)

PHP
慕森王 2021-08-28 16:27:47
我正在學習 php,其中一項任務是創建某個類。我已經嘗試了很多,但仍然有錯誤。任務:創建類User,將有$name和$age私人財產和公共$site財產,其價值應在類的構造函數,方法來設置getFullInfo(),將返回(不能打?。。?$this->name at the age of $this->age is a user of $this->site。構造函數應該檢查輸入年齡,如果它大于130或小于0設置$age值unset(就像一個字符串)。成功創建實例后,構造函數應該打印User was successfully created!.我使用 PHP 5。我學習的站點不適用我的解決方案版本。希望,你會幫助我^^<?php class User{    private $name;    private $age;    public $site;    function __construct($q,$w,$e){        echo "User was successfully created!";        $this->name=$q;        $this->site=$e;        if($w>130 || $w<0){            unset($w);        };        $this->age=$w;    }    public function getFullInfo(){        return "$this->name at the age of $this->age is a user of $this->site";    } } ?>
查看完整描述

3 回答

?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

任務說如果年齡超出范圍,您應該將其設置為字符串unset。打電話unset($w)不會那樣做。將其更改為:

$w = "unset";


查看完整回答
反對 回復 2021-08-28
?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

你很接近。將變量、參數和參數命名為有意義的名稱。


如果它大于 130 或小于 0,則將 $age 值設置為未設置(就像字符串一樣)


大概意思 $this->age = 'unset';


<?php 

class User

{

    private $name;

    private $age;

    public $site;


    function __construct($name, $site, $age)

    {

        echo "User was successfully created!";


        $this->name = $name;

        $this->site = $site;


        if ($age > 130 || $age < 0) {

            $this->age = 'unset';

        } else {

            $this->age = $age;

        }


    }


    public function getFullInfo()

    {

        return "$this->name at the age of $this->age is a user of $this->site";

    }

}


$user = new User('Tony McTony', 'Tree House', 35);


echo $user->getFullInfo();


// Output: User was successfully created!Tony McTony at the age of 35 is a user of Tree House



查看完整回答
反對 回復 2021-08-28
?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

我認為這里是:


 return "$this->name at the age of $this->age is a user of $this->site";


// it should be : (i think)


return $this->name . "at the age of " . $this->age . " is a user of " . $this->site;




希望能幫助到你


查看完整回答
反對 回復 2021-08-28
  • 3 回答
  • 0 關注
  • 184 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號