What is the difference between class and instance methods?

后端 未结 18 2154
说谎
说谎 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:32

    All the technical details have been nicely covered in the other answers. I just want to share a simple analogy that I think nicely illustrates the difference between a class and an instance:

    enter image description here

    A class is like the blueprint of a house: You only have one blueprint and (usually) you can't do that much with the blueprint alone.

    enter image description here

    An instance (or an object) is the actual house that you build based on the blueprint: You can build lots of houses from the same blueprint. You can then paint the walls a different color in each of the houses, just as you can independently change the properties of each instance of a class without affecting the other instances.

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

    Like most of the other answers have said, instance methods use an instance of a class, whereas a class method can be used with just the class name. In Objective-C they are defined thusly:

    @interface MyClass : NSObject
    
    + (void)aClassMethod;
    - (void)anInstanceMethod;
    
    @end
    

    They could then be used like so:

    [MyClass aClassMethod];
    
    MyClass *object = [[MyClass alloc] init];
    [object anInstanceMethod];
    

    Some real world examples of class methods are the convenience methods on many Foundation classes like NSString's +stringWithFormat: or NSArray's +arrayWithArray:. An instance method would be NSArray's -count method.

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

    An update to the above answers, I agree instance methods use an instance of a class, whereas a class method can be used with just the class name.

    There is NO more any difference between instance method & class method after automatic reference counting came to existence in Objective-C.

    For Example[NS StringWithformat:..] a class method & [[NSString alloc] initwihtformat:..] an instance method, both are same after ARC

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

    Class methods can't change or know the value of any instance variable. That should be the criteria for knowing if an instance method can be a class method.

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

    Adding to above answers

    Class method will work on class, we will use this for general purpose where like +stringWithFormat, size of class and most importantly for init etc

    NSString *str = [NSString stringWithFormat:@"%.02f%%",someFloat]; 
    

    Instance Method will work on an instance of a class not on a class like we are having two persons and we want to get know the balance of each separately here we need to use instance method. Because it won't return general response. e.g. like determine the count of NSSArray etc.

    [johnson getAccountBalance];
    [ankit getAccountBalance];
    
    0 讨论(0)
  • 2020-11-21 12:38

    So if I understand it correctly.

    A class method does not need you to allocate instance of that object to use / process it. A class method is self contained and can operate without any dependence of the state of any object of that class. A class method is expected to allocate memory for all its own work and deallocate when done, since no instance of that class will be able to free any memory allocated in previous calls to the class method.

    A instance method is just the opposite. You cannot call it unless you allocate a instance of that class. Its like a normal class that has a constructor and can have a destructor (that cleans up all the allocated memory).

    In most probability (unless you are writing a reusable library, you should not need a class variable.

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