标签:
M文件
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *per = [[Person alloc]init]; //初始化 直接调用Person.m文件中的- (instancetype)init
[per display];
Person *per2 = [[Person alloc]initWithPid:444 andName:@"任" andAge:50]; //可以重新赋值
[per2 display];
}
return 0;
}
Person.h文件
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int _pid;
NSString *_name;
int _age;
}
- (instancetype)init;
-(void)display;
-(instancetype)initWithPid:(int)pid andName:(NSString*)name andAge:(int)age;
@end
Person.m文件
#import "Person.h"
@implementation Person
- (instancetype)init{
self = [super init];
if (self) { //self = [super init]
self->_pid = 1;
self->_name = @"ren";
self->_age = 20;
}
return self;
}
-(void)display{
NSLog(@"%d,%@,%d",_pid,_name,_age);
}
-(instancetype)initWithPid:(int)pid andName:(NSString*)name andAge:(int)age{ //如果不想写死 可以写一个赋值
if (self=[super init]) {
self->_pid = pid;
self->_name = name;
self->_age = age;
}
return self;
}
@end
标签:
原文地址:http://www.cnblogs.com/WJR12/p/5031049.html