: (冒號)@interface Triangle : NSObject@private 以外的實例變數// 父類別
@interface Triangle : NSObject
@property double a, b, c;
- (instancetype)initWithA:(double)a B:(double)b C:(double)c;
- (double)area;
@end
@implementation Triangle
- (instancetype)initWithA:(double)a B:(double)b C:(double)c {
self = [super init];
if (self) { _a = a; _b = b; _c = c; }
return self;
}
- (double)area {
double s = (_a + _b + _c) / 2;
return sqrt(s * (s - _a) * (s - _b) * (s - _c));
}
@end
// 子類別:零程式碼繼承所有功能
@interface TriangleSub : Triangle @end
@implementation TriangleSub @end
// 使用
TriangleSub *sub = [[TriangleSub alloc] initWithA:10 B:11 C:12];
NSLog(@"Area: %g", [sub area]); // 正常運作
核心規則:方法調用根據物件實際類別 (isa 指向的類別) 決定,而非變數宣告類別。
@interface TestClass : NSObject - (void)test; @end
@implementation TestClass - (void)test { NSLog(@"TestClass"); } @end
@interface SubA : TestClass @end
@implementation SubA - (void)test { NSLog(@"SubA"); } @end
@interface SubB : TestClass @end
@implementation SubB - (void)test { NSLog(@"SubB"); } @end
// 測試
TestClass *obj = [[SubB alloc] init];
[obj test]; // 輸出: SubB (依實際類別 SubB)
// 編譯期類別 TestClass 僅作型別檢查、自動完成提示
id 型別核心思想:將其他物件作為自身成員 (has-a 關係),組合複雜功能,而非繼承 (is-a)。
// SDK 提供元件
@interface KeyBoard : NSObject @end
@interface BigMouse : NSObject @end
@interface Monitor : NSObject @end
@interface IntelCPU : NSObject @end
// 電腦類別:合成元件
@interface Computer : NSObject
@property (strong, nonatomic) KeyBoard *aKeyBoard;
@property (strong, nonatomic) BigMouse *aMouse;
@property (strong, nonatomic) Monitor *aMonitor;
@property (strong, nonatomic) IntelCPU *aCPU;
@end
@implementation Computer
- (instancetype)init {
self = [super init];
if (self) {
self.aKeyBoard = [[KeyBoard alloc] init];
self.aMouse = [[BigMouse alloc] init];
self.aMonitor = [[Monitor alloc] init];
self.aCPU = [[IntelCPU alloc] init];
}
return self;
}
@end
| 分類 | 關鍵字 | 說明 | 預設值 |
|---|---|---|---|
| 讀寫權限 | readonly | 唯讀 (僅生成 getter) | readwrite |
| readwrite | 可讀寫 (生成 getter/setter) | ||
| 記憶體管理 | assign | 單純賦值,不持有 (基本型別、weak 參考) | assign |
| strong | 強持有 (ARC 保留) | ||
| weak | 弱參考 (物件釋放自動置 nil) | ||
| copy | 複製 (不可變副本,NSString/Block 必用) | ||
| 執行緒安全 | atomic | 原子存取 (鎖保護),效能較低 | atomic |
| nonatomic | 非原子 (高效能),多執行緒需自行同步 |
// 基礎元件
@interface Kongtiao : NSObject - (void)zhileng; @end
@implementation Kongtiao - (void)zhileng { NSLog(@"製冷中..."); } @end
@interface ChuJiaQuan : NSObject - (void)chuJQ; @end
@implementation ChuJiaQuan - (void)chuJQ { NSLog(@"除甲醛中..."); } @end
// 高級空調:繼承空調 + 合成除甲醛模組
@interface NewKongtiao : Kongtiao
@property (strong, nonatomic) ChuJiaQuan *cJQ;
@end
@implementation NewKongtiao
- (instancetype)init {
self = [super init]; // 呼叫父類別初始化
if (self) {
self.cJQ = [[ChuJiaQuan alloc] init]; // 合成元件
}
return self;
}
// 重寫並擴充父類別方法
- (void)zhileng {
[super zhileng]; // 關鍵:呼叫父類別原始實作
[self.cJQ chuJQ]; // 新增功能
}
@end
[super init]:初始化鏈,確保父類別狀態就緒[super method]:在覆寫方法中呼叫父類別實作 (擴充而非完全取代)super.ivar:直接存取父類別實例變數 (較少用,破壞封裝)| 場景 | 推薦 | 理由 |
|---|---|---|
| is-a 關係明確 (Dog 是 Animal) | 繼承 | 語意正確、自動獲得介面 |
| has-a / uses-a 關係 (Car 有 Engine) | 合成 | 低耦合、執行期可替換元件 |
| 需要多型行為 | 繼承 + 協定 | 統一介面、不同實作 |
| 功能組合、橫向擴充 | 合成 | 避免繼承層級爆炸 |