Objective-C/iOS的新手,我无法弄清楚如何访问子类变量.所以我创建了一个Person类,其中包含以下代码:
@interface Person : NSObject @property NSSTring *firstName;
我已经将它发送到viewController中,一切正常.
接下来我右键单击Person类,并创建了Person的子类并添加了以下内容:
#import "Person.h" @interface Student : Person @property NSInteger studentID; @end
在viewController中;
Person *person = [[Person alloc] init]; person.firstName = @"name"; person.lastName = @"lastname";
一切正常,但我无法弄清楚如何访问子类Student的变量studentID?
我已经尝试初始化子类但它似乎没有工作.
干杯
你不能从人那样做.您初始化了一个人,而不是学生.子类可以访问父属性,但不能反过来.你可以这样做:
Student *person = [[Student alloc] init]; person.firstName = @"name"; person.lastName = @"lastname"; person.sudentId = 123;