写了一个最间单的类似Java Bean的例子
和Java比较有如下不同:
创建新的对象
- 分配内存
- 初始化
Person *p;
p = [[Person alloc] newPersonWithName:@”Ting Wang” withYear
NSInteger *) 31];
alloc用来分配内存, newPersonWithName是一个初始化的方法,这里可以是定义的任何方法。
在objective c里, 每个对象要用 * 来标记他是个对象的引用。内部数据, int structure enum不需要*, 他们不是对象。
- (id) newPersonWithName:(NSString *) n withYear:(NSInteger *) y
{
if (self = [super init]) {
year = y;
name = n;
}
return self;
}
多个参数的方法, 每个参数要字母加上”:”
可以写的可读性很好,例如
p = [[Person alloc] newPersonWithName:@”Ting Wang” withYear
NSInteger *) 31];
就可以读出来。
总的来说, objective c还是很像c

A more Cocoa-style method name would be -initWithName:year:.
June 6, 2009 @ 10:42 pm
thanks for the comment.
I think newPersonWithName:withYear is just like a sentence.
I first impress of objective c is, the program in objective c can be read like in english.
June 7, 2009 @ 1:30 am