zoukankan      html  css  js  c++  java
  • oc基础复习08-OC的类方法

    直接上代码 代码里面有注释

     1 #import <Foundation/Foundation.h>
     2 
     3 @interface Animal : NSObject
     4 //类方法使用 “+”开头 不带参数的
     5 +(void)eat;
     6 
     7 +(void)eat02:(NSString *)name;
     8 
     9 @end
    10 
    11 
    12 @implementation Animal
    13 
    14 +(void)eat
    15 {
    16     NSLog(@"Animal is eating!");
    17 };
    18 
    19 +(void)eat02:(NSString *)name
    20 {
    21     NSLog(@"Animal is eating %@", name);
    22 }
    23 
    24 @end
    25 
    26 int main(int argc, const char * argv[]) {
    27     @autoreleasepool {
    28         // insert code here...
      //看到这里没有 直接使用类名调用方法
    29 [Animal eat]; 30 [Animal eat02:@"leaf"]; 31 } 32 return 0; 33 }

     对象方法

    1.减号开头

    2.只能由对象来调用

    3.对象方法可以使用成员变量

    类方法:

    加号开头 有类直接调用 类方法不能访问成员变量(实例变量)

    类方法的好处

    不依赖于对象 执行效率高

  • 相关阅读:
    __str__
    __call__
    私有成员
    @property
    静态方法
    静态字段
    cut qcut
    hive 函数大全
    sklearn 中的Countvectorizer/TfidfVectorizer保留长度小于2的字符方法
    numpy教程:随机数模块numpy.random
  • 原文地址:https://www.cnblogs.com/greenboy/p/4613387.html
Copyright © 2011-2022 走看看