This article describes the PHP object-oriented member methods. If you need a friend, refer to the following method to use a column to read members: you can write a column by yourself to deepen your understanding.
Such a requirementHope that people can talk and do arithmetic..., so you need to use the member method:
1. Add the speak member method. The output is James.
2. Add the jisuan member method to calculate the result from 1 +... + 1000
3. Modify the jisuan member method. This method can receive a number n and calculate the result of 1 +... + n.
4. add a member method to calculate the sum of two numbers
Reference code:
The Code is as follows:
Class Person {
Public $ name;
Public $ age;
// Add member Method
Public function speak (){
Echo "I am James ";
}
Public function jisuan (){
// Calculate the result from 1 +... + 1000
$ Result = 0;
For ($ I = 1; $ I <= 1000; $ I ++ ){
$ Result + = $ I;
}
// Returns the calculated result ruturn.
Return $ result;
}
Public function jisuan2 ($ n ){
// Calculate the result from 1 +... + n
$ Result = 0;
For ($ I = 1; $ I <= $ n; $ I ++ ){
$ Result + = $ I;
}
// Return
Return $ result;
}
// Calculate the sum of 2 numbers
Public function add ($ num1, $ num2 ){
Return $ num1 + $ num2;
}
}
$ Person1 = new Person;
// Speak
$ Person1-> speak ().'
';
// Computing
Echo'
'. $ Person1-> jisuan ();
// Echo'
'. $ Person1-> jisuan (100 );
// With Parameters
Echo: '. $ person1-> jisuan2 (5 );
// Calculate the sum of 2 numbers
Echo"
50 + 50 = ". $ person1-> add (50, 51 );
?>