从今天开始学习ios,入门第一天(Object-C学习笔记)

来源:互联网 发布:数据挖掘工程师考证 编辑:程序博客网 时间:2024/06/07 22:34

1.在Object-C中定义类,并实例化对象

//在SimpleClass.h文件中这样定义

@interface SimpleClass :NSObject

{

    @public

    int firstInt;

    int secondInt;

}

- (id)initWithFirstInt:(int)firstIntValue secondInt:(int)secondIntValue;

@end

//在SimpleClass.m文件中这样定义

#import "SimpleClass.h"

@inplementation SimpleClass

- (id)initWithFirstInt:(int) firstIntValue secondInt:(int) secondIntValue//用id类型作为init方法的返回值是cocoa的习惯用法

{

self = [super init];//用来指明接受消息的对象实例

if(!self)//OBC中的常用写法,如果一个变量不指向任何事物,它的值就是nil,nil就是0

return nil;

firstInt = firstIntValue;

secondInt  = secondIntValue;

return self;

}

@end

SimpleClass *aSimpleClassInstance=[[SimpleClass alloc] initWithFirstInt:1 secondInt:2];//实例化对象

2.在Object-C中定义工厂方法

在Object-C中,如果方法名称前面是一个(+)号,则表明该方法是一个静态(static)方法,方法名称前面是一个减号(-)表明该方法是一个实例方法。

+ (id)simpleClassWithFirstInt : (int)firstIntValue secondInt :(int) secondValue


0 0
原创粉丝点击