在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
objective-c在xcode6下的例子:
#import <Foundation/Foundation.h> //基础库,类似C中的stdlib typedef enum eType {type1 = 1,type2,type3} ETYPE; //OC中对enum、#define、typedef的使用与在C中基本无差异 @interface Teacher : NSObject{ int tid; NSString *name; ETYPE type; }
#import "Teacher.h" #import <Foundation/Foundation.h> @implementation Teacher @synthesize tid,name,type; //标示所声明的属性 -(void)setName:(NSString *)inputname andId:(int)inputtid andType:(ETYPE)inputtype{ self.tid = inputtid; //self相当于this self.name = inputname; self.type = inputtype; } -(void) print{ NSLog(@"%@ and %i and type %i", self.name,self.tid,self.type); } @end
#import <Foundation/Foundation.h> #import "Student.h" #import "Teacher.h" int main(int argc, const char * argv[]) { @autoreleasepool { Student *tmp1 = [[Student alloc]init]; [tmp1 setName:@"fredric" andId:18]; //id object id data; //指向NSObject的指针,下面这个例子可以实现类似多态的功能 data = tmp1; [data print]; } return 0; } |
请发表评论