1. 程式人生 > >黑馬程式設計師Objective-C筆記:封裝,繼承,多型

黑馬程式設計師Objective-C筆記:封裝,繼承,多型

例子:
#import 
  
  
@interface Animal : NSObject
- (void)eat;
@end
  
@implementation Animal
- (void)eat
{
    NSLog(@"Animal---Eating something!!");
}
@end
  
@interface Dog : Animal
@end
  
@implementation Dog
- (void)eat
{
    NSLog(@"Dog--Eating something!!");
}
@end
  
int main()
{
    //多種形態
  //  Dog *d = [Dog new];     //Dog型別
    Animal *a = [Dog new];  //呼叫一個物件方法時,呼叫的是真實物件方法
   // NSObject *n = [Animal new];
    [a eat];
    return 0;
}
輸出結果為:Dog--Eating something