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