[关闭]
@fiy-fish 2015-07-18T19:32:34.000000Z 字数 722 阅读 1377

day06-04-多态

Objective-c


  1. // main.m
  2. // day06-04-多态
  3. //
  4. // Created by Aaron on 15/7/8.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "Dog.h"
  9. int main(int argc, const char * argv[]) {
  10. @autoreleasepool {
  11. Dog *d = [[Dog alloc] init];
  12. [d show];
  13. }
  14. return 0;
  15. }

  1. #import <Foundation/Foundation.h>
  2. @interface Animal : NSObject
  3. {
  4. @private
  5. NSString *_name;
  6. }
  7. -(void)showName;
  8. @end

  1. #import "Animal.h"
  2. @implementation Animal
  3. -(instancetype)init
  4. {
  5. if(self =[super init])
  6. {
  7. _name = @"xxx";
  8. }
  9. return self;
  10. }
  11. //-(void)setName:(NSString *)name
  12. //{
  13. // _name = name;
  14. //}
  15. -(NSString *)name
  16. {
  17. return _name;
  18. }
  19. -(void)showName
  20. {
  21. NSLog(@"%@",[self name]);
  22. }
  23. @end

  1. #import "Animal.h"
  2. @interface Dog : Animal
  3. -(void)show;
  4. @end

  1. #import "Dog.h"
  2. @implementation Dog
  3. -(void)show
  4. {
  5. NSLog(@"jsahdak");
  6. [super showName];
  7. }
  8. @end

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注