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. SPOJ GSS1 Can you answer these queries I ——线段树

    [题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...

  2. 刷题总结——学姐的逛街计划(vijos1891费用流)

    题目: doc 最近太忙了, 每天都有课. 这不怕, doc 可以请假不去上课.偏偏学校又有规定, 任意连续 n 天中, 不得请假超过 k 天. doc 很忧伤, 因为他还要陪学姐去逛街呢. 后来, ...

  3. 刷题总结——spoj1812(后缀自动机+DP)

    题目: A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is t ...

  4. 【最优K叉树】hdu 5884 Sort

    http://acm.hdu.edu.cn/showproblem.php?pid=5884 参考:https://www.cnblogs.com/jhz033/p/5879452.html [题意] ...

  5. Log4j2同步异步性能比较以及教程和问题(坑)汇总

    线程数:500个   每个线程日志输出次数: 500次 log4j2其实有两个输出异步日志的方式:AsyncLogger和AsyncAppend 他两的区别在于: AsyncLogger使用的是无锁高 ...

  6. c/s委托练习

    今天玩了玩C/S开发,也随便练习了很久不用的委托 父窗体中写的代码 #region 委托与事件传递    public delegate void TextChangedHandler(string ...

  7. *LOJ#6227. 「网络流 24 题」最长k可重线段集问题

    $n \leq 500$条平面上的线段,问一种挑选方法,使得不存在直线$x=p$与挑选的直线有超过$k$个交点,且选得的直线总长度最长. 横坐标每个点开一个点,一条线段就把对应横坐标连一条容量一费用( ...

  8. Codeforces 849B Tell Your World (计算几何)

    题目链接 Tell Your World 题意 给出N个点(i, xi),问是否存在两条平行的直线,使得每一个点恰好在两条直线的其中一条上. 每条直线必须穿过至少一个点. 考虑每个点和第1个点的斜率, ...

  9. codevs——1690 开关灯

    1690 开关灯 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description YYX家门前的街上有N( ...

  10. java集合系列之HashMap源码

    java集合系列之HashMap源码 HashMap的源码可真不好消化!!! 首先简单介绍一下HashMap集合的特点.HashMap存放键值对,键值对封装在Node(代码如下,比较简单,不再介绍)节 ...