利用 OC 可以的面向對象可以創建一個類具體步驟
- 聲明接口 注意繼承 NSObject 父類,NSObject 是 Foundation 框架裏面的,所以需要 import Foundation 框架
- 聲明實現
- 調用 new
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
@public
int wheel;
int peed;
}
@end
@implementation Car
@end
int main(){
Car *p = [Car new];
p -> wheel = 5;
p -> speed = 120;
return 0;
}
編譯
cc -c 01.m
cc 01.m -framework Foundation
注意:新聲明的成員變量是 protected 的,不能用指針訪問
instance variable 'wheel' is protected
p -> wheel = 5;
這時候需要給成員變量上面加上 @public