OC-改错题

1,类方法中不能访问成员变量

2,id后不能加*(因为id相当于NSObject *)

3,id类型的变量不能用点语法

4,类对象只能调用类方法,不能调用对象方法

1.description
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property int age;
@end

@implementation Person
// 类方法中不能访问成员变量
//+ (NSString *)description
- (NSString *)description
{
    return [NSString stringWithFormat:@"_age=%d", _age];
}
@end

// 1个错误

/*-------------------------牛B的分隔线-------------------------*/

2.id、SEL、类对象
#import <Foundation/Foundation.h>
@interface Person : NSObject
- (void)test;
@property int age;
@end

@implementation Person
- (void)test
{
    // 会引发死循环错误
    //[self performSelector:_cmd];
}
@end

int main()
{
    // id后面不能加上*
    //id *p = [[Person alloc] init];
    id p = [[Person alloc] init];
    [p setAge:10];
    
    // id类型的变量不能用点语法
    // p.age = 10;
    
    Class c = [Person class];
    // 类对象只能调用类方法,不能调用对象方法
    //[c test];
    
    return 0;
}

 

posted @ 2015-11-11 21:12  yhidr  阅读(173)  评论(0编辑  收藏  举报