Read the member method with one of the following: You can write it yourself and deepen your understanding.
such a demand ; I hope that people can talk, do arithmetic problems ..., so you need to use the member method:
1, add Speak member method, output I am xiaoming
2, add Jisuan member method, can calculate from 1+. +1000 of the results
3, modifies the Jisuan member method, this method may receive a number N, calculates 1+. The result of +n
4, add member method, you can calculate the number of two and
Reference code:
Copy Code code as follows:
<?php
Class person{
Public $name;
Public $age;
Add Member Method
Public Function speak () {
echo "I am xiaoming";
}
Public Function Jisuan () {
Calculated from 1+. +1000 of the results
$result = 0;
For ($i =1 $i <=1000; $i + +) {
$result + + $i;
}
Ruturn the results of the calculation back
return $result;
}
Public Function jisuan2 ($n) {
/calculation from 1+. Results of +n
$result =0;
for ($i =1 $i <= $n $i + +) {
$result + + $i;
}
//return
return $result;
}
Calculate the number of 2 and
Public function Add ($num 1, $num 2) {
return $num 1+ $num 2;
}
}
$person 1=new person;
People talking
$person 1->speak (). ' <br/> ';
Calculation
Echo ' <br/> ' $person 1->jisuan ();
Echo ' <br/> ' $person 1->jisuan (100);
With parameters of
Echo ' Calculates the result: '. $person 1->jisuan2 (5);
Calculate the number of 2 and
echo "<br/>50+50=". $person 1->add (50,51);
?>