zoukankan      html  css  js  c++  java
  • 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.
    终于明白,“喜欢”是一种莫大的能量!
  • 相关阅读:
    ShiroConfig V2.0
    MyRealm V2.0(注:加上了权限字符串)
    ShiroUtils通用工具包
    ResourcesConfig实现配置资源路径
    MyRealm V1.0
    ShiroConfig V1.0
    MySQL
    Git实战
    scala中函数简单使用记录
    scala中Trait简单使用
  • 原文地址:https://www.cnblogs.com/tml839720759/p/3570009.html
Copyright © 2011-2022 走看看