PHP object-oriented-member Method

Source: Internet
Author: User

Using a column to read and understand Members: You can write and understand it by yourself.

This is a requirement. Hope that people can talk and do arithmetic questions... so that they 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

ReferenceCode:

<? PHP  Class  Person {  Public   $ Name  ;  Public   $ Age  ;  //  Add member Method         Public   Function  Speak (){  Echo "I'm 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 two numbers          Public   Function Add ( $ Num1 , $ Num2  ){  Return   $ Num1 + $ Num2  ;}}  $ Person1 = New Person;  //  People speak      $ Person1 -> Speak (). '<br/>' ;  //  Computing      Echo '<Br/> '. $ Person1 -> Jisuan ();  //  Echo '<br/>'. $ person1-> jisuan (100); // with Parameters      Echo 'The calculated result is :'. $ Person1 -> Jisuan2 (5);  //  Calculate the sum of two numbers      Echo "<Br/> 50 + 50 = ". $ Person1 -> Add (50, 51 ); ?>

 

 

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.