PHP-based object-oriented member methods-PHP Tutorial

Source: Internet
Author: User
Detailed description of PHP object-oriented member methods. Using a column to read and understand members: you can write and understand it by yourself. Such a demand; hope that people can talk and do arithmetic ......., in this way, you need to use a column to read the member method: you can write it 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 );
?>


Bytes. Such a demand; hope that people can talk, do arithmetic problems ......, so you need to use...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.