2015-09-27 64 views
-1

我無法將數組作爲參數傳遞到類中,我不知道該怎麼做。我寫這篇文章是因爲它告訴我這篇文章主要是代碼,我不能發表,所以我需要添加更多的細節。無法將數組作爲參數傳遞給類

人:

class Human{ 
    protected $height; 
    protected $weight; 
    protected $age; 
    protected $gender; 
    protected $nat; 

    public function __construct($height,$weight,$age,$gender,$nat){ 
     $this->height = $height; 
     $this->weight = $weight; 
     $this->age = $age; 
     $this->gender = $gender; 
     $this->nat = $nat;  
    } 

    //toString method 
    public function __toString(){ 
     return 
     "height: ".$this->height. 
     " meters;<br /> weight: ".$this->weight. 
     ";<br /> age: ".$this->age. 
     ";<br /> gender: ".$this->gender. 
     ";<br /> nationality: ".$this->nat; 
    } 

    //GET methods 
    public function getHeight(){ 
     return $this->height; 
    } 
    public function getWeight(){ 
     return $this->weight; 
    } 
    public function getAge(){ 
     return $this->age; 
    } 
    public function getGender(){ 
     return $this->gender; 
    } 
    public function getNat(){ 
     return $this->nat; 
    } 

    //SET methods 
    public function setHeight($height){ 
     $this->height = $height; 
    } 
    public function setWeight($weight){ 
     $this->weight = $weight; 
    } 
    public function setAge($age){ 
     $this->age = $age; 
    } 
    public function setGender($gender){ 
     $this->gender = $gender; 
    } 
    public function setNationality($nat){ 
     $this->nationality = $nat; 
    } 
} 

學生:

class Student extends Human{ 
    private $uni; 
    private $year; 
    private $faculty; 

    public function __construct($height,$weight,$age,$gender,$nat,$uni,$year,$faculty){ 
     parent::__construct($height,$weight,$age,$gender,$nat,$uni,$year,$faculty); 
     $this->uni = $uni; 
     $this->year = $year; 
     $this->faculty = $faculty;  
    } 

    public function __toString(){ 
     return 
     "height: ".$this->height. 
     " meters;<br /> weight: ".$this->weight. 
     ";<br /> age: ".$this->age. 
     ";<br /> gender: ".$this->gender. 
     ";<br /> nationality: ".$this->nat. 
     ";<br /> university: ".$this->uni. 
     ";<br /> year: ".$this->year. 
     ";<br /> faculty: ".$this->faculty; 

    } 

    //GET methods 
    public function getUni(){ 
     return $this->uni;  
    } 
    public function getYear(){ 
     return $this->year;  
    } 
    public function getFaculty(){ 
     return $this->faculty; 
    } 

    //SET methods 
    public function setUni($uni){ 
     $this->uni = $uni;   
    } 
    public function setYear($year){ 
     $this->year = $year;  
    } 
    public function setFaculty($faculty){ 
     $this->faculty = $faculty;  
    } 

    //adding year of studying to student 
    public function add($year){ 
     $this->year = $year; 
     $year++; 
     echo $year;;  
    } 
} 

程序員:

class Programmer extends Human{ 
    public $lang = array("PHP/SQL", "Java", "C#", "HTML/CSS", "Javascript"); 
    private $exp; 
    private $salary; 
    private $company; 

    public function __construct($height,$weight,$age,$gender,$nat,$lang,$exp,$salary,$company){ 
     parent::__construct($height,$weight,$age,$gender,$nat,$lang,$exp,$salary,$company); 
     $this->lang = $lang; 
     $this->exp = $exp; 
     $this->salary = $salary; 
     $this->company = $company;  
    } 

    public function __toString(){ 
     return 
     "height: ".$this->height. 
     " meters;<br /> weight: ".$this->weight. 
     ";<br /> age: ".$this->age. 
     ";<br /> gender: ".$this->gender. 
     ";<br /> nationality: ".$this->nat. 
     ";<br /> languages: ".$this->lang; 
     ";<br /> experience: ".$this->exp; 
     ";<br /> salary: ".$this->salary; 
     ";<br /> company: ".$this->company; 

    } 

    //get aarray 
    public function getArr($lang){ 
     for($i = 0; $i < count($lang); $i++){ 
      echo "element s indeksom $i raven: ".$i."<br />"; 
      echo $i;  
     } 
    } 
} 

代碼:

$human = new Human(1.75, 80, 22, "male", "australian"); 
echo $human; echo "<br />"; 
echo "-------------------<br />"; 

$student = new Student(1.70, 90, 24, "female", "british", "harvard", 3, "IT"); 
echo $student; echo "<br />"; 
echo "-------------------<br />"; 

echo $student->getYear(); echo "<br />"; 
echo $student->add(3); echo "<br />"; 
echo "-------------------<br />"; 

//i cant pass array as a parameter 
$programmer = new Programmer(1.80, 70, 27, "male", "british", 2, 5,10000,"Google"); 
echo $programmer; echo "<br />"; 
echo $programmer->getArr(1); 
+1

你在說什麼數組? –

+0

public $ lang = array(「PHP/SQL」,「Java」,「C#」,「HTML/CSS」,「Javascript」); –

+0

又是什麼?這個數組是如何與你提供的類連接的? –

回答

1

我不太明白你的問題,但我會嘗試用你的代碼修正一些問題。

你說你不能將數組傳遞給函數。您的類中可以使用數組的唯一函數是getArr($lang)

您在調用getArr(1)時遇到的問題是,1被認爲是您想要迭代的數組。但是1不是一個數組,1只是一個整數。

所以,你應該再想想你想在你的函數getArr做什麼:

  • 顯示陣列,其傳遞的參數與指標
  • 表演元素從$lang類屬性的所有元素,通過作爲論據。

更新:

好了,所以如果你想getArr功能顯示從預定義的類屬性$lang那麼所有的語言,你不需要把它作爲參數傳遞,作爲類的實例已經意識到這一點,這樣你就可以相應地重寫你的函數:

public function getArr() { 
    // use $this->lang to access $lang property 
    for ($i = 0; $i < count($this->lang); $i++) { 
     // access $i element of array with []-notation 
     echo "element s indeksom $i raven: " . $this->lang[$i] . "<br />"; 
    } 
} 

另一個更新

您的public function __construct($height,$weight,$age,$gender,$nat,$lang 對於類Programmer$lang的說法。並且您傳遞了值2。因此,$this->lang變成2,這也不是一個數組。你爲什麼要這麼做?

+0

我剛剛寫了1,因爲它要求一個參數,而且即時通訊遠不瞭解OOP,因爲你看到我甚至不知道我需要我的程序需要做什麼的正確問題,但生病嘗試,所以,我需要程序輸出的值,我傳入的對象,但其中一個類的參數是數組有預定義的值,我需要這些預定義的值被呼應/打印或任何你可以調用它,所以它顯示程序員知道的語言的數量, –

+0

我需要數組來放置它的值,而不要求我將值傳遞給構造函數,因爲我已經聲明併爲該數組賦值,但如何獲取他們打印? –

+0

更新了我的答案。 –

0

你想要這樣做嗎?

$programmer = new Programmer(1.80, 70, 27, "male", "british", array('HTML/CSS', 'PHP/SQL'), 5, 10000, "Google"); 
+0

是的,我從理論上試圖做到這一點,但無論如何,我得到了我想要的結果,只是從一個對象中移除數組,然後自己迴應它 –

相關問題