Use an alternative to read the Member method: You can write your own writing, deepen understanding.
such a demand ; Hope that people can speak, do arithmetic problems ..., so you need to use the member method:
1. Add Speak member method, output I'm xiaoming.
2, add Jisuan member method, you can calculate from 1+. Results of +1000
3, modify the Jisuan member method, the method can receive a number n, calculate 1+. Results of +n
4. Adding the Add Member method, you can calculate two number of and
Reference code:
Copy the Code code as follows:
Class person{
Public $name;
Public $age;
Add Member Method
Public Function speak () {
echo "I am xiaoming";
}
Public Function Jisuan () {
Calculated from 1+. Results of +1000
$result = 0;
for ($i =1; $i <=1000; $i + +) {
$result + = $i;
}
Return the result of the calculation Ruturn
return $result;
}
Public Function jisuan2 ($n) {
Calculated from 1+. Results of +n
$result = 0;
for ($i =1; $i <= $n; $i + +) {
$result + = $i;
}
Return
return $result;
}
Calculates the 2-digit and
Public function Add ($num 1, $num 2) {
return $num 1+ $num 2;
}
}
$person 1=new person;
People talking
$person 1->speak (). '
';
Calculation
Echo '
'. $person 1->jisuan ();
Echo '
'. $person 1->jisuan (100);
With the parameter
The result of Echo ' calculation is: '. $person 1->jisuan2 (5);
Calculates the 2-digit and
echo "
50+50= ". $person 1->add (50,51);
?>