About "self"

Class method can't refer derectly to instance variables. Within the body of  a class method, self refers to the class object itself. For example:

@interface Myclass : NSObject 
+ (id)classMethod;
@end
 
Implementation of the classMethod like this, let's call it method_A:
 
+ (id)classMethod
{
      return [[[self alloc] init] autorelease];
}
 
the "self" in the body  of the method  named "classMethod"  refers to the class object rather than the instance variable.  
Also you can do like this, let's call it method_B:
 
+ (id)classMethod
{
      return [[[MyClass alloc] init] autorelease];
}
 
So what's the difference between the two methods above:
Imagine that you create a subclass of Myclass, maybe like this:
 
@interface MySubClass: MyClass 
@end
 
And now you want to  create a instance of MySubClass by class method like "[MySubClass classMethod]" ,but wait, if you use  method_A, that's OK,  you create a instance of MySubClass successfully,if you use method_B, eh...and you can see that method_B return a instance of MyClass, that's not what you wanted.
终于明白,“喜欢”是一种莫大的能量!
原文地址:https://www.cnblogs.com/tml839720759/p/3570009.html