ios开发(五):基本语法 Class

来源:互联网 发布:java程序设计哪本好 编辑:程序博客网 时间:2024/05/16 19:15

class分成两部分,一个是interface,一个是implementation

INTERFACE

1) 继承

@interface ClassName : ItsSuperclass
// Method and property declarations.
@end

2) 私有公有函数

+ alloc;  // Class method, 没有返回数值,默认是id

- (void)setRadius:(float)aRadius; // instance method

3)属性(property)

@property (attributes) Type propertyName;

instance variables(大括号内的都是instance variable,这些变量都是私有的)

@interface ClassName : ItsSuperclass
{
     // Instance variable declarations.
}
// Method and property declarations.
@end

4) @class Rectangle, Circle;

class的声明,声明之后就不需要再import文件了,这个和C++非常类似。


IMPLEMENTATION

1) @implementation ClassName

{
     // Instance variable declarations.
}
// Method definitions.
@end


2) operator (->)

@interface Sibling : NSObject
{
    Sibling *twin;
    int gender;
    struct features *appearance;
}

- makeIdenticalTwin
{
    if ( !twin ) {
        twin = [[Sibling alloc] init];
        twin->gender = gender;  // 访问他本身就使用->
        twin->appearance = appearance;
    }
    return twin;
}

3) scope

@interface Worker : NSObject
{
    char *name;
@private
    int age;
    char *evaluation;
@protected
    id job;
    float wage;
@public
    id boss;
}


SELF SUPER

[self setOrigin:someX :someY];

[super setOrigin:someX :someY];