zoukankan      html  css  js  c++  java
  • Object c 基础知识

    文件类型说明:
    .h     头文件,用于定义类、实例变量及类中的方法等定义信息(interface)。
    .m    源文件,定义方法体,可实现objce-c和c方法(implementation)。
    .mm   c++源文件

    引用头文件:
    可通过#import关键字引入想要使用的头文件,该关键字作用通#include相同,不过import会确保每个文件只会被引用一次。

    object-c中类的定义有两部分:interface和implementation
    interface部分包含类的定义以及实例变量和方法。interfache通常定义在.h(头)文件中;
    implementation部分包含方法的实现部分(方法体),implementation通常定义在.m(源)文件中。

    复制代码
    
    1 @interface MyClass : NSObject
    2 {
    3     int count;
    4     id data;
    5     NSString* name;  
    6 }
    7 -(id)initWithString:(NSString*)aName;
    8 +(MyClass*)createMyClassWithString:(NSString*)aName;
    9 @end
    
    复制代码

    在上述代码中,包含了以下信息:
    名为MyClass的类,继承于Cocoa框架中的NSObject类;
    定义了3个成员变量:count、data和name(@protected)
    以及声明了2个方法

    实例变量也可以放到源文件中定义,如:

    复制代码
    
     1 // 源文件中的:NSObject为可选,一般情况下不用写
     2 @implenmentation MyClass :NSObject
     3 {
     4     int count;
     5     int data;
     6     NSString *name;
     7 }
     8 
     9 -(id)initWithString:(NSString*)aName
    10 {
    11     ...  
    12 }
    
    复制代码

    但需要注意的是,若成员变量定义在源文件中而非头文件中,则变量的访问权限都为@private.

    object-c支持强类型和弱类型两种方式定义一个对象变量:
        强类型定义时变量类型即类命,且为指针类型
        弱类型定义使用id型定义

    
    1 MyClass *myObject;    // 强类型定义
    2 id myObject;    //弱类型定义
    

    方法的定义和引用:
    Object-C中的方法同其它面向对象语言一样,分两种方法:实例方法(-)和类方法(+)(静态方法)。
    实例方法需要通过类的实例去调用,而静态方法可直接通过类名去调用。

    
    1 - (void)insertObject:(id)anObject atIndex:(NSUInter)index;
    

    上述代码中定义了一个名为"insertObject:atIndex"的实例方法,该方法无返回值,并定义了2个形参,类型分别为id和NSUInter。

    在定义一个方法时,也可以不指定参数名,如:

    
    1 // 声明一个没有参数名的方法
    2 -(int) set: (int) n: (int) d;
    3 
    4 // 方法调用
    5 [object set : 4 : 5];
    

    方法的调用:
    在Object-C中,调用一个方法叫做发送消息,方法调用语句定义在中括号"[]"中:

    
    1 [myArray insertObject:anObject atIndex:0];
    

    为了避免生成大量的临时变量,Object-C允许嵌套调用方法,使一个方法的返回值当作另一个方法的参数,如:

    
    1 [[myAppObject theArray] inserObject:[myAppObject objectToInsert] atIndex:0];
    

    成员变量:
    默认情况下,Object-C中的成员变量为
    @protected,可通过@public,@private关键字修改成员变量的可见性(只有成员变量拥有这些属性)。

    复制代码
    
     1 @interface MyObject : NSObject
     2 {
     3     // 成员变量要定义在大括号中
     4     @public int n1;
     5     @private int n2;
     6     @protected int n3;
     7 }
     8 ......
     9 @end
    10 
    11 // 通过类实例访问public的成员变量:
    12 MyObject *mo = [[MyObject alloc] init];
    13 mo->n1;
    
    复制代码

    若要访问受保护的成员变量,需要在类中定义相应的get和set方法,通过类实例调用这些方法来达到访问或设置这些成员变量的目的。

    访问器 
    在Object-C中,提供了@property和@synthesize两个关键字,通过这两个关键字,可以高效的为每个指定的成员变量设定set方法和get方法,称为属性

    复制代码
    
     1 // 头文件
     2 @interface MyObject : NSObject
     3 {
     4     @property int n1, n2;
     5 }
     6 ...
     7 @end
     8 
     9 // 源码文件
    10 @implementation MyObject
    11 
    12 @synthesize n1, n2;
    13 ...
    14 @end 
    
    复制代码

    在上述代码中,Object-C会自动创建4个方法:n1、n2、setN1和setN2。
    可通过对象实例直接调用这4个方法

    
    MyObject *mo = [[MyObject alloc] init];
    int l_n1, l_n2;
    [mo setN1:4];
    [mo setN2:8];
    l_n1 = [mo n1];
    l_n2 = [mo n2];
    

    Object-C也支持使用"."符号来访问properties

    
    1 mo.n1 = 5;
    

    这种方式同样适用于调用方法

    变量范围

    Object-C中的变量范围同C一样,在{}中定义的变量属于本地变量,只能在{}中使用,本地变量默认初始化值为nil。同样方法的传值方式是传值引用,但当传递对象时是地址引用。
    Object-C中也支持静态变量,通过static关键字,如:

    
    1 static int n = 0;
    

    静态变量默认初始值为0,同C一样。注:静态变量同样只能在定义的范围内使用。

    self变量相当于java中的this。

    继承

    Ojbect-C中的继承同java相同,都是单一继承关系,要继承一个类,可写作:

    
    1 @interface ClassA : NSObject
    2 
    3 @end
    

    其中NSObject类是最顶层类,一般所有类的最终父类都是它。

    在子类中可以访问父类中的非private变量的实例变量和方法(静态方法和实例方法)。
    注:只能访问非private属性的成员变量,说明该变量必须定义在interface中(类的声明中),因为在implementation中声明的成员变量全部都是private。

    在子类中通过super访问父类中的方法,如:

    
    1 [super init];
    

     

    NSObject类常用方法:

    方法名 说明

    -(BOOL) isKindOfClass: class-object 

    Is the object a member of class-object or a descendant? 

    -(BOOL) isMemberOfClass: class-object 

    Is the object a member of class-object? 

    -(BOOL) respondsToSelector: selector 

    Can the object respond to the method specified by selector

    +(BOOL) instancesRespondToSelector: selector

     

    Can instances of the specified class respond to selector? 

     

    +(BOOL)isSubclassOfClass: class-object

     

    Is the object a subclass of the specified class?

     

    -(id) performSelector: selector 

     

    Apply the method specified by selector

     

    (id) performSelector: selector withObject: object 

     

    Apply the method specified by selector passing the argument object. 

     

    -(id) performSelector: selector withObject: object1 withObject: object2 

     

    Apply the method specified by selector with the arguments object1 and object2. 

    class-object是一个类对象,通过class方法生成,可通过类名和实例名调用class方法生成,如:

    复制代码
    
     1 [Square class]; // Square 是一个类名
     2 [mySquare class]; // mySquare 是一个实例
     3 
     4 // 验证两个实例是否属于同一个类
     5 if ([obj1 class] == [obj2 class])
     6 {
     7     ...
     8 }
     9 
    10 // 验证myFan是否属于Fraction,即是否是Fraction的实例
    11 [myFan isMemberOfClass: [Fraction class]] 
    
    复制代码

    selector是一个SEL类型的变量(C中的函数指针),直接通过@selector生成 ,如:

    复制代码
    
    // 为alloc方法生成一个SEL类型变量
    @selector (alloc)
    
    @selector (setTo:over:)
    
    // 查看Fraction类中是否含有setTo:over方法
    // 该方法也去父类中查找
    [Fraction instancesRespondToSelector: @selector(setTo:over)];
    
    复制代码

    通过performSelector动态调用函数:

    复制代码
    
     1 @implementation Fraction
     2 -(void) setTO:(int) a Over:(int)b
     3 {
     4     ...
     5 }
     6 -(void) print
     7 {
     8     // 通过performSelector方法调用setTo:Over
     9     // 传递2个整数参数4和5
    10     [self performSelector:@selector(setTo:Over) withObject:4 withObject:5]
    11 }
    12 @end
    
    复制代码
    
    1 if ([graphicObject respondsToSelector: action] == YES)
    2     [graphicObject performSelector: action]
    3 else
    4     // error handling code here
    

     异常

    使用@try和@catch捕获异常:

    复制代码
    
    1 @try
    2 {
    3     ...
    4 } @catch(NSException *exception)
    5 {
    6     ...
    7 }
    8 // 可选的放置一个@finally,该语句无论是否有异常发生,都会执行
    
    复制代码

     编写类的构造函数

    可为一个类编写多个不同的初始化函数,实例化类时通过指定不同的初始化函数执行不同的初始化操作

    
    1 -(Fraction *) initWith: (int) n over: (int) d {
    2     self = [super init];
    3     if (self)
    4         [self setTo: n over: d];
    5     return self;
    6 }
    

    重写init方法

    复制代码
    
     1 
     6 
     7 -(id) init
     8 {
     9     return [self initWith:0 over:0];
    10 }
    
    复制代码

    在类外部定义的变量会成为全局变量,在任何类和方法中(包括其它文件)中都可以访问这个变量。

    关键字extern可用来在类和方法内部访问全局变量,同PHP的global类似,但切记:
    extern是声明,而不是定义。
    在变量前使用extern关键字,并不会为该变量分配内存,仅仅是将该变量标识为全局变量,所以不可以在使用extern定义变量时赋值。

    枚举

    Object-C支持枚举类型,格式为:

    复制代码
    
     1 // 定义枚举
     2 enum flag { false, true };
     3 
     4 // 声明两个枚举变量,该枚举变量的值只能是true或false
     5 enum flag endOfData, matchFound;
     6 
     7 // 定义枚举
     8 enum direction { up, down, left = 10, right };
     9 
    10 //
    11 enum boolean { no = 0, false = 0, yes = 1, true = 1 };
    
    复制代码

     

  • 相关阅读:
    《Java架构师的第一性原理》12计算机基础之计算机网络
    《Java架构师的第一性原理》11计算机基础之操作系统
    《Java架构师的第一性原理》34分布式计算之分布式消息队列(AcitveMQ、RabbitMQ、RocketMQ、Kafka、ZeroMQ)
    《Java架构师的第一性原理》32分布式计算之分布式缓存第1篇Redis
    《Java架构师的第一性原理》41存储之MySQL深入浅出全网最全,没有之一
    《Java架构师的第一性原理》41存储之MySQL第11篇数据库调优
    C++拷贝赋值operator=
    C++浅拷贝与深拷贝
    C++左值和右值
    C++函数的哑元参数
  • 原文地址:https://www.cnblogs.com/songfeixiang/p/3733752.html
Copyright © 2011-2022 走看看