一、instancetype是什么?
例如,有一个Person类: @interface Person : NSObject @property NSString *name; @property int age;
- (Person *)person; //按照规范,应该有一个同名的类方法,用来返回一个该类的对象 @end
@implementation Person ++ (Person *)person { return [Person new]; } @end 这时候,如果我想拿到一个Person对象的话,那就: #import int main() { Person *p1 = [Person person]; } 请问一个问题,这个person()方法,可不可以被子类Student继承到?答案是,可以! 这时候,我调用这个person()方法: Student *s1 = [Student person]; 返回的不是一个Student对象! 现在,如何能让这个方法,通过Person来调,返回的就是Person对象,通过Student来调,返回的就是Student对象? 1)+ (Person *)person;这个方法的返回值类型能不能写(Person *)类型,不能