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 3270 博物馆 ——概率DP 高斯消元

    用$F(i,j)$表示A在i,B在j的概率. 然后很容易列出转移方程. 然后可以高斯消元了! 被一个问题困扰了很久,为什么起始点的概率要加上1. (因为其他博客上都是直接写成-1,雾) 考虑初始状态是 ...

  2. es6 箭头函数 map、find

    var  value = arr.map(function (x) {return x * x}); const arr = [1,2,3,4]; const value = arr.map(x =& ...

  3. GFS, HDFS, Blob File System架构对比

    分布式文件系统很多,包括GFS,HDFS,淘宝开源的TFS,Tencent用于相册存储的TFS (Tencent FS,为了便于区别,后续称为QFS),以及Facebook Haystack.其中,T ...

  4. CI(CodeIgniter)框架中的增删改查操作

    我们创建一个模型( 项目目录/models/),请注意:模型名与文件名相同且必须继承数据核心类CI_Model,同时重载父类中的构造方法 CodeIgniter的数据函数类在 \models\User ...

  5. bootstrap-datatables

    刚写到datatimepicker的时候想到这个问题. 这可以说是我接触到的第一个功能如此齐全的一款依赖型插件.我把依赖于别人库的插件这么称呼. 首先上官网:http://datatables.clu ...

  6. 在echars上发布的半圆环形图

    http://gallery.echartsjs.com/editor.html?c=xBJvoMcPfz&v=1

  7. formSubmit

    精简代码: <form name='form0001' method="post"> .... <li id="view"><a ...

  8. AC日记——砝码称重 洛谷 P2347

    题目描述 设有1g.2g.3g.5g.10g.20g的砝码各若干枚(其总重<=1000), 输入输出格式 输入格式: 输入方式:a1 a2 a3 a4 a5 a6 (表示1g砝码有a1个,2g砝 ...

  9. es6 递归 tree

    function loop(data) { let office = data.map(item => { if(item.type == '1' ||item.type == '2') { i ...

  10. FireDac心得

    usesFireDAC.Phys.MySQL, FireDAC.Stan.Def, FireDAC.DApt, FireDAC.Comp.Client, FireDAC.Comp.UI, FireDA ...