object-c 子类覆盖父类属性 详解

来源:互联网 发布:打击电信网络诈骗作文 编辑:程序博客网 时间:2024/06/01 10:44

////  main.m//  test_OC_chind_super_one////  Created by admin on 1/7/16.//  Copyright © 2016 jeffasd. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        // insert code here...        NSLog(@"Hello, World!");    }        Student *student = [Student new];        NSString *string = student.name;        student.identifier = @"set identifier";        NSLog(@"the student identifier is %@", student.identifier);        NSLog(@"the string is %@", string);    //    student.name = @"987";        return 0;}

////  Person.h//  test_OC_chind_super_one////  Created by admin on 1/7/16.//  Copyright © 2016 jeffasd. All rights reserved.//#import <Foundation/Foundation.h>@interface Person : NSObject@property(nonatomic, copy)NSString *identifier;@end

////  Person.m//  test_OC_chind_super_one////  Created by admin on 1/7/16.//  Copyright © 2016 jeffasd. All rights reserved.//#import "Person.h"@implementation Person- (instancetype)init{    self = [super init];    if (self != nil) {        _identifier = @"12";    }    return self;}@end

////  Student.h//  test_OC_chind_super_one////  Created by admin on 1/7/16.//  Copyright © 2016 jeffasd. All rights reserved.//#import "Person.h"@interface Student : Person@property(nonatomic, copy)NSString *identifier;@property(nonatomic, readonly)NSString *name;@end

////  Student.m//  test_OC_chind_super_one////  Created by admin on 1/7/16.//  Copyright © 2016 jeffasd. All rights reserved.//#import "Student.h"@implementation Student@synthesize identifier = _identifier;//@dynamic identifier;    //使用@dynamic后必须要自己实现getter和setter方法- (instancetype)init{    self = [super init];    if (self != nil) {//        [self setName:@"456"];        self.name = @"456";    }    return self;}//- (NSString *)identifier//{//    return @"3";//}//- (NSString *)name//{//    return//}- (void)setName:(NSString *)name{    _name = name;}//- (void)setIdentifier:(NSString *)identifier//{//    [super setIdentifier:identifier];//    //    //}- (void)setIdentifier:(NSString *)identifier{    [super setIdentifier:identifier];//    _identifier = @"set identifier";    _identifier = identifier;}@end

2016-01-07 13:18:11.967 test_OC_chind_super_one[2002:51547] Hello, World!

2016-01-07 13:18:11.968 test_OC_chind_super_one[2002:51547] the student identifier is set identifier

2016-01-07 13:18:11.969 test_OC_chind_super_one[2002:51547] the string is 456

Program ended with exit code: 0


0 0
原创粉丝点击