Class methods and Object methods in OC base--OC

Source: Internet
Author: User

PS: Personal feeling is somewhat similar to the static and non-static methods of C #, just a little bit similar. Mingjie said don't always compare with the language previously learned, but personally think, compare it can deepen the impression it. The point is that you can really tell apart!

The object method in OC

1. start with a minus sign "-"

2. Only the object can be called, no object, this method cannot be executed at all

3. Object methods can access instance variables (i.e. member variables)

Second, the class method in OC

1. start with the plus sign "+"

2. Can only be called with the class name, the object cannot be called

3. Instance variables (i.e. member variables) cannot be accessed in a class method

4. Application: When you do not need to access member variables, try to use the class method

It is worth noting that class methods and object methods can have the same name, but use it with caution!

Three, code example:

1 /*2 Design a calculator class3 1. Class Name: Caculator4 2. Method:5 * Return to pi:3.146 * Calculates the square of a value7 * Calculation of two values and8  */9 Ten #import<Foundation/Foundation.h> One  A //Statement of the calculator - @interfaceCaculator:nsobject - //declaration of the method the+ (Double) pi; -+ (Double) Pingfang: (Double) number; -+ (Double) SumOfNum1: (Double) NUM1 andNum2: (Double) num2; - @end +  - //the implementation of the calculator + @implementationCaculator A  at //implementing the methods declared in @interface -+ (Double) Pi - { -     return 3.14; - } -  in+ (Double) Pingfang: (Double) number - { to     returnNumber *Number ; + } -  the+ (Double) SumOfNum1: (Double) NUM1 andNum2: (Double) num2 * { $     returnNUM1 +num2;Panax Notoginseng } - @end the  +  A intMain () the { + [Caculator Pi]; -      $[Caculator Pingfang:Ten]; $      -[Caculator SUMOFNUM1:TenANDNUM2:5]; -     return 0; the}

Class methods and Object methods in OC base--OC

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.