我無法將數組作爲參數傳遞到類中,我不知道該怎麼做。我寫這篇文章是因爲它告訴我這篇文章主要是代碼,我不能發表,所以我需要添加更多的細節。無法將數組作爲參數傳遞給類
人:
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);
你在說什麼數組? –
public $ lang = array(「PHP/SQL」,「Java」,「C#」,「HTML/CSS」,「Javascript」); –
又是什麼?這個數組是如何與你提供的類連接的? –