在OC中結構體有時候也作為對象的屬性類的定義#import typedef struct{ int year; int month; int day;} Date;@interface Student : NSObject{ @public NSString *_nam...
在OC中結構體有時候也作為對象的屬性
類的定義
#import <Foundation/Foundation.h> typedef struct{ int year; int month; int day; } Date; @interface Student : NSObject { @public NSString *_name; Date _birthday; } -(void) say; @end
類方法
#import "Student.h" @implementation Student -(void) say{ NSLog(@"name=%@;year=%d,month=%d,day=%d",_name,_birthday.year,_birthday.month,_birthday.day); } @end
對象的實例化及方法的實現
#import <Foundation/Foundation.h> #import "Student.h" int main(int argc, const char * argv[]) { @autoreleasepool { Student *stu=[[Student alloc] init]; stu->_name=@"喵星人"; //方法1 // stu->_birthday=(Date){1995,2,15}; //方法2 // stu->_birthday.year=1995; // stu->_birthday.month=2; // stu->_birthday.day=25; //方法3 Date date={1995,2,25}; stu->_birthday=date; [stu say]; } return 0; }