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.

随机推荐

  1. 【bzoj3545】[ONTAK2010]Peaks 线段树合并

    [bzoj3545][ONTAK2010]Peaks 2014年8月26日3,1512 Description 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路 ...

  2. msp430项目编程53

    msp430综合项目---扩展项目三53 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结

  3. OC-为何用copy修饰block

    简单来说,block就像一个函数指针,指向我们要使用的函数. 就和函数调用一样的,不管你在哪里写了这个block,只要你把它放在了内存中(通过调用存在这个block的方 法或者是函数),不管放在栈中还 ...

  4. P1067 多项式输出 (模拟)

    题目描述 一元nn次多项式可用如下的表达式表示: 其中,a_i x^i 称为i次项,ai​ 称为i次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式: 多项式中自变量 ...

  5. [bzoj1110][POI2007]砝码Odw_贪心

    bzoj-1110 POI-2007 砝码Odw 参考博客:http://hzwer.com/4761.html 题目大意:在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件 ...

  6. T1008 选数 codevs

    http://codevs.cn/problem/1008/ 题目描述 Description 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整 ...

  7. 一个球,初始高度100,每次落下回弹一半高度,求第n次落下球走的距离

    def get_height(n): if n==1: eturn 150 return 100+sum([200*pow(0.5,i) for i in range(1,n)])+100*pow(0 ...

  8. 【Todo】【读书笔记】Career Cup 150笔记

    下载了第五版:/Users/baidu/Documents/Data/Interview/算法与数据结构/<CareerCup+Top+150+Questions+5th.pdf> 参考这 ...

  9. 【Todo】一些scala的实验 & 与Java的混合

    另外,如果要支持 java 和 scala混合build,可以看看这篇文章: http://www.cnblogs.com/yjmyzz/p/4694219.html Scala和Java实现Word ...

  10. function 之 arguments 、call 、apply

    1.arguments arguments.length为函数实参个数,arguments.callee引用函数自身. arguments他的特性和使用方法 特性: arguments对象和Funct ...