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. BZOJ 1297: [SCOI2009]迷路 [矩阵快速幂]

    Description windy在有向图中迷路了. 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. 现在给出该有向图,你能告诉windy总共有多少种不同 ...

  2. BZOJ 3856: Monster【杂题】

    Description Teacher Mai has a kingdom. A monster has invaded this kingdom, and Teacher Mai wants to ...

  3. 【kmp+最小循环节】poj 2406 Power Strings

    http://poj.org/problem?id=2406 [题意] 给定字符串s,s=a^n,a是s的子串,求n最大是多少 [思路] kmp中的next数组求最小循环节的应用 例如 ababab ...

  4. MySQL 中 key, primary key ,unique key,index的区别

    一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_n ...

  5. mysql 5.7版本目录无data文件夹的解决办法

    安装mysql 5.7+版本时,若发现因根目录下,缺少data文件夹的情况, ***请不要去拷贝其他版本的data文件夹!*** 因为此操作会出现很多潜在问题:比如我遇到的执行show variabl ...

  6. 基于Android的远程视频监控系统(含源码)

    基本过程是android作为socket客户端将采集到的每一帧图像数据发送出去,PC作为服务器接收并显示每一帧图像实现远程监控.图片如下(后来PC端加了个拍照功能)... (PS.刚学android和 ...

  7. 文件重定向,getline()获取一样,屏幕输出流,格式控制符dec,oct,hex,精度控制setprecision(int num),设置填充,cout.width和file(字符),进制输入

     1.在window下的命令重定向输出到文件里 2.将内容输入到某个文件里的方式:命令<1.txt (使用1.txt中的命令) 3.读取文件里的名,然后将命令读取最后输出到文件里.命令< ...

  8. [scrapy]Item Loders

    Items Items就是结构化数据的模块,相当于字典,比如定义一个{"title":"","author":""},i ...

  9. ZT:CSS实现水平|垂直居中漫谈

    有篇博客园网友‘云轩奕鹤’的文章不错,转载在这里以供需要时查阅. http://www.cnblogs.com/jadeboy/p/5107471.html

  10. 重新认识一遍JavaScript

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...