自定义构造方法
注意:父类的方法交给父类的方法来处理,子类的方法处理子类自己独有的属性
<span style="font-size:14px;">#import <Foundation/Foundation.h>
@interface Person : NSObject
@property NSString* name;
@property int age;
//声明自定义构造方法
-(instancetype)initWithName:(NSString*)_name and:(int)_age;
@end
@implementation Person
//实现自定义构造方法
-(instancetype)initWithName:(NSString*)_name and:(int)_age{
if(self = [super init]){
self->_name = _name;
self->_age = _age;
}
return self;
}
@end
int main(){
//初始化一个Person对象
Person *p = [[Person alloc] initWithName:@"demo" and:20];
NSLog(@"p.name = %@ ,p.age = %d",p.name,p.age);
return 0;
}
</span>