封装:将类的成员变量用方法来管理,不再用@public继承:子类可以引用父类的方法和成员变量
多态:当一个函数以父类的类型的参数时,子类也可以作为参数传进去
#import <Foundation/Foundation.h>
@interface Car: NSObject {
int wheels; int speed;
} - (void)run; - (void)carBeep;
@end
@implementation Car - (void)run { NSLog(@"车子跑起来了"); }
- (void)carBeep { NSLog(@"car 响了"); } @end
@interface Aodi : Car - (void)carBeep; @end @implementation Aodi - (void)carBeep { NSLog(@"aodi 响了"); } @end
@interface Baoma : Car - (void)carBeep; @end @implementation Baoma - (void)carBeep { NSLog(@"baoma 响了"); } @end void leep(Car *c) { [c carBeep]; }
int main() { @autoreleasepool { Baoma *baoma = [Baoma new]; leep(baoma); Aodi *aodi = [Aodi new]; leep(aodi); //NSLog(@"%d个轮子的车子的速度是%d",car1->wheels,car1->speed); }
return 0; }
|