What is the difference between class and instance methods?

后端 未结 18 2186
说谎
说谎 2020-11-21 11:55

What\'s the difference between a class method and an instance method?

Are instance methods the accessors (getters and setters) while class methods are pretty much ev

相关标签:
18条回答
  • 2020-11-21 12:39

    An instance method applies to an instance of the class (i.e. an object) whereas a class method applies to the class itself.

    In C# a class method is marked static. Methods and properties not marked static are instance methods.

    class Foo {
      public static void ClassMethod() { ... }
      public void InstanceMethod() { ... }
    }
    
    0 讨论(0)
  • 2020-11-21 12:40

    Also remember, the same idea applies to variables. You will come across terms like static, member, instance, class and so on when talking about variables the same as you would for methods/functions.

    It seems the common term in the Obj-C community is ivar for instance variable, but I am not an Obj-C guy, yet.

    0 讨论(0)
  • 2020-11-21 12:42

    CLASS METHODS


    A class method typically either creates a new instance of the class or retrieves some global properties of the class. Class methods do not operate on an instance or have any access to instance variable.


    INSTANCE METHODS


    An instance method operates on a particular instance of the class. For example, the accessors method that you implemented are all instance methods. You use them to set or get the instance variables of a particular object.


    INVOKE


    To invoke an instance method, you send the message to an instance of the class.

    To invoke a class method, you send the message to the class directly.


    Source: IOS - Objective-C - Class Methods And Instance Methods

    0 讨论(0)
  • 2020-11-21 12:45

    In Objective-C all methods start with either a "-" or "+" character. Example:

    @interface MyClass : NSObject
    // instance method
    - (void) instanceMethod;
    
    + (void) classMethod;
    @end
    

    The "+" and "-" characters specify whether a method is a class method or an instance method respectively.

    The difference would be clear if we call these methods. Here the methods are declared in MyClass.

    instance method require an instance of the class:

    MyClass* myClass = [[MyClass alloc] init];
    [myClass instanceMethod];
    

    Inside MyClass other methods can call instance methods of MyClass using self:

    -(void) someMethod
    {
        [self instanceMethod];
    }
    

    But, class methods must be called on the class itself:

    [MyClass classMethod];
    

    Or:

    MyClass* myClass = [[MyClass alloc] init];
    [myClass class] classMethod];
    

    This won't work:

    // Error
    [myClass classMethod];
    // Error
    [self classMethod];
    
    0 讨论(0)
  • 2020-11-21 12:46

    Take for example a game where lots of cars are spawned.. each belongs to the class CCar. When a car is instantiated, it makes a call to

    [CCar registerCar:self]
    

    So the CCar class, can make a list of every CCar instantiated. Let's say the user finishes a level, and wants to remove all cars... you could either: 1- Go through a list of every CCar you created manually, and do whicheverCar.remove(); or 2- Add a removeAllCars method to CCar, which will do that for you when you call [CCar removeAllCars]. I.e. allCars[n].remove();

    Or for example, you allow the user to specify a default font size for the whole app, which is loaded and saved at startup. Without the class method, you might have to do something like

    fontSize = thisMenu.getParent().fontHandler.getDefaultFontSize();
    

    With the class method, you could get away with [FontHandler getDefaultFontSize].

    As for your removeVowels function, you'll find that languages like C# actually have both with certain methods such as toLower or toUpper.

    e.g. myString.removeVowels() and String.removeVowels(myString) (in ObjC that would be [String removeVowels:myString]).

    In this case the instance likely calls the class method, so both are available. i.e.

    public function toLower():String{
      return String.toLower();
    }
    
    public static function toLower( String inString):String{
     //do stuff to string..
     return newString;
    }
    

    basically, myString.toLower() calls [String toLower:ownValue]

    There's no definitive answer, but if you feel like shoving a class method in would improve your code, give it a shot, and bear in mind that a class method will only let you use other class methods/variables.

    0 讨论(0)
  • 2020-11-21 12:52

    Class methods are usually used to create instances of that class

    For example, [NSString stringWithFormat:@"SomeParameter"]; returns an NSString instance with the parameter that is sent to it. Hence, because it is a Class method that returns an object of its type, it is also called a convenience method.

    0 讨论(0)
提交回复
热议问题