zoukankan      html  css  js  c++  java
  • 【iOS 知识汇】oc static

    oc static 跟java有所区别。

    1.全局只有在本类可以访问。出文件就不可访问了,区别与java.

    2.类方法可以访问static 变量。oc + 方法类似java静态方法。

    3.一个静态变量所以实例对象共用。分配在堆区内存。相同java。

    4.方法中声明static 变量。只有在方法中访问。同样一个静态变量所以实例对象共用。(严重区别与java,java在方法中不可以声明静态 static)

    5.(重要 )静态变量只初始化一次。如果方法中声明的也是如此。

    6.static 变量不初始化,均有默认值。比如int a ,默认值为0 ;

    #import <Foundation/Foundation.h>
    static int a ;//只能在@interface和@end外面定义。
    @interface StaticTest : NSObject
    //static int b ;
    -(void)st ;
    +(void)printA;
    @end
    static int c = 1000 ;
    #import "StaticTest.h"
    static int d ;
    @implementation StaticTest
    -(instancetype)init{
        self = [super init];
        a++ ;
        return self;
    }
    
    -(void)st{
        static int e ;
        NSLog(@"e:%d",++e);
    }
    
    +(void)printA{
        NSLog(@"a:%d",a);
    }
    @end
    #import <Foundation/Foundation.h>
    #import "StaticTest.h"
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // insert code here...
            NSLog(@"Hello, World!");
            
            
           
            StaticTest* test =[StaticTest new];
            NSLog(@"a:%@",[StaticTest class]);
            
            [test st];
            [test st];
            [test st];
            [test st];
            StaticTest* test1 =[StaticTest new];
           
            [test1 st];
            [StaticTest printA];
        }
        return 0;
    }
    2020-06-13 15:06:33.670 test[1955:43303] Hello, World!
    2020-06-13 15:06:33.672 test[1955:43303] a:StaticTest
    2020-06-13 15:06:33.673 test[1955:43303] e:1
    2020-06-13 15:06:33.673 test[1955:43303] e:2
    2020-06-13 15:06:33.673 test[1955:43303] e:3
    2020-06-13 15:06:33.673 test[1955:43303] e:4
    2020-06-13 15:06:33.673 test[1955:43303] e:5
    2020-06-13 15:06:33.673 test[1955:43303] a:2
    Program ended with exit code: 0
  • 相关阅读:
    Centos7运维(1)-为什么在centos7配置了静态IP不生效还是分配一个动态ip给我??
    docker 常用命令
    docker 安装
    centos6.8 修改yum安装镜像源
    开发自己的composer package
    修改mysql密码
    MySQL密码的恢复方法
    nginx配置文件说明
    天猫优惠券
    mysql的一些心得
  • 原文地址:https://www.cnblogs.com/mamamia/p/13114662.html
Copyright © 2011-2022 走看看