便利构造器的概念:
- 便利构造器是一种快速创建对象的方式。
- 它本质上是把初始化方法做了一次封装,方便外界使用。
- 便利构造器是一个类方法。
- 通常以类名开头,返回值仍然是id类型。
在初始化的同时给成员变量赋值
初始化,产生一个对象,将其赋值给实例变量(对象)
注:
- 便利构造器的命名规范:必须以类名开头。
-
person的便利构造器为:[[[Person alloc] init] autorelease];
[[Person alloc] initWithAge:age andWithName:name];//有两个属性的写法,
便利初始化的调用:
Person *p = [Person initWithAge: 10 andWithName: @"jason"];// 在init时就赋值了age和name属性
便利初始化方法声明:
-(类名 *)initWith成员变量名:(类型)参数名
实现部分:
- (Student *)initWithName:(NSString *)newName{
self = [super init];//通过父类调用init初始化方法,产生一个对象,此处的self就是类的对象
//判断是否初始化成功,未初始化之前,self = nil
if (self) {
//如果初始化成功,则进行相关操作(比如,给成员变量赋值)
name = newName;
if (self) {
//如果初始化成功,则进行相关操作(比如,给成员变量赋值)
name = newName;
}
//将初始化完成后的对象返回
return self;
}
便利构造器方法声明:
+(类名 *)类名小写With变量名
+ (Student *)studentWithName:(NSString *)newName andAge:(int)newAge;
便利构造器实现:
+(Student *)studentWithName:(NSString *)newName andAge:(int)newAge{
//初始化一个对象
//初始化一个对象
//原始的初始化方法
Student *stu = [[Student alloc] init];
//给成员变量赋值(类方法里无法直接使用成员变量,因为成员变量属于对象)
stu->name = newName;
stu->age = newAge;
//给成员变量赋值(类方法里无法直接使用成员变量,因为成员变量属于对象)
stu->name = newName;
stu->age = newAge;
//便利初始化
// Student *stu = [[Student alloc] initWithName:newName andAge:newAge];
NSLog(@"---%@",stu->name);
//将初始化完成后的对象返回
return stu;
NSLog(@"---%@",stu->name);
//将初始化完成后的对象返回
return stu;
}
便利构造器的调用,
[类名 方法名 ];
Student *stu = [Student studentWithName:@"老三" andAge:20];
例:
类接口:
#import
@interface Car : NSObject
{
NSString *mark;
NSString *color;
float price;
int seat;
}
-(Car*)initWithMark:(NSString*)newMark andColor:(NSString*)newColor andPrice:(float)newPrice andSeat:(int)newSeat;
-(void)introduction;
@interface Car : NSObject
{
NSString *mark;
NSString *color;
float price;
int seat;
}
-(Car*)initWithMark:(NSString*)newMark andColor:(NSString*)newColor andPrice:(float)newPrice andSeat:(int)newSeat;
-(void)introduction;
@end
类实现:
#import "Car.h"
@implementation Car
-(Car*)initWithMark:(NSString *)newMark andColor:(NSString *)newColor andPrice:(float)newPrice andSeat:(int)newSeat
{
self = [super init];
if (self) {
mark = newMark;
color = newColor;
price = newPrice;
seat = newSeat;
}
return self;
}
-(void)introduction
{
NSLog(@"我是一辆%@汽车我是%@颜色我的售价是%.2f我有%d个座位!",mark,color,price,seat);
}
@end
@implementation Car
-(Car*)initWithMark:(NSString *)newMark andColor:(NSString *)newColor andPrice:(float)newPrice andSeat:(int)newSeat
{
self = [super init];
if (self) {
mark = newMark;
color = newColor;
price = newPrice;
seat = newSeat;
}
return self;
}
-(void)introduction
{
NSLog(@"我是一辆%@汽车我是%@颜色我的售价是%.2f我有%d个座位!",mark,color,price,seat);
}
@end
主程序:
#import
#import "Car.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Car *car = [[Car alloc]initWithMark:@"Audi" andColor:@"yellow"andPrice:100000000 andSeat:7];
[car introduction];
}
return 0;
#import "Car.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Car *car = [[Car alloc]initWithMark:@"Audi" andColor:@"yellow"andPrice:100000000 andSeat:7];
[car introduction];
}
return 0;
}
运行结果:
2015-10-20 15:33:07.707 10.20 下午课堂练习[2177:100206] 我是一辆Audi汽车我是yellow颜色我的售价是100000000.00我有7个座位!
运用便利构造器的例子:
(1)
类接口:
#import
@interface AudiCar : NSObject
{
NSString *color;
float price;
int seat;
}
-(AudiCar*)initWithColor:(NSString*)newcolor andPrice:(float)newprice andSeat:(int)newseat;
+(AudiCar*)audiCarWithColor:(NSString*)newcolor andPrice:(float)newprice andSeat:(int)newseat;
-(void)introduction;
@interface AudiCar : NSObject
{
NSString *color;
float price;
int seat;
}
-(AudiCar*)initWithColor:(NSString*)newcolor andPrice:(float)newprice andSeat:(int)newseat;
+(AudiCar*)audiCarWithColor:(NSString*)newcolor andPrice:(float)newprice andSeat:(int)newseat;
-(void)introduction;
@end
类实现:
#import "AudiCar.h"
@implementation AudiCar
-(AudiCar *)initWithColor:(NSString *)newcolor andPrice:(float)newprice andSeat:(int)newseat
{
self = [super init];
if(self)
{
color = newcolor;
price = newprice;
seat = newseat;
}
return self;
}
+(AudiCar *)audiCarWithColor:(NSString *)newcolor andPrice:(float)newprice andSeat:(int)newseat
{
AudiCar *car = [[AudiCar alloc]initWithColor:newcolor andPrice:newprice andSeat:newseat];
return car;
}
-(void)introduction
{
NSLog(@"我是%@颜色价格是%f有%d个座位!",color,price,seat);
}
@implementation AudiCar
-(AudiCar *)initWithColor:(NSString *)newcolor andPrice:(float)newprice andSeat:(int)newseat
{
self = [super init];
if(self)
{
color = newcolor;
price = newprice;
seat = newseat;
}
return self;
}
+(AudiCar *)audiCarWithColor:(NSString *)newcolor andPrice:(float)newprice andSeat:(int)newseat
{
AudiCar *car = [[AudiCar alloc]initWithColor:newcolor andPrice:newprice andSeat:newseat];
return car;
}
-(void)introduction
{
NSLog(@"我是%@颜色价格是%f有%d个座位!",color,price,seat);
}
@end
主程序:
#import
#import"AudiCar.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
AudiCar *car = [AudiCar audiCarWithColor:@"yellow" andPrice:41000.3 andSeat:7];
[car introduction];
}
return 0;
#import"AudiCar.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
AudiCar *car = [AudiCar audiCarWithColor:@"yellow" andPrice:41000.3 andSeat:7];
[car introduction];
}
return 0;
}
运行结果:
2015-10-20 17:48:46.997 10.20
便利构造器
[3097:139957]
我是
yellow
颜色价格是
41000.300781
有
7
个座位!
(2)
类接口:
#import
@interface Teacher : NSObject
{
NSString *name;
NSString *sex;
int age;
NSString *marriage;
}
-(void)sayHi;
-(void)gotowork;
-(Teacher*)initWithName:(NSString*)newname andSex:(NSString*)newsex andAge:(int)newage andMarriage:(NSString*)newmarriage;
+(Teacher*)teacherWithName:(NSString*)newname andSex:(NSString*)newsex andAge:(int)newage andMarriage:(NSString*)newmarriage;
@interface Teacher : NSObject
{
NSString *name;
NSString *sex;
int age;
NSString *marriage;
}
-(void)sayHi;
-(void)gotowork;
-(Teacher*)initWithName:(NSString*)newname andSex:(NSString*)newsex andAge:(int)newage andMarriage:(NSString*)newmarriage;
+(Teacher*)teacherWithName:(NSString*)newname andSex:(NSString*)newsex andAge:(int)newage andMarriage:(NSString*)newmarriage;
@end
类实现:
#import "Teacher.h"
@implementation Teacher
-(void)sayHi
{
NSLog(@"Goodmorning!");
}
-(void)gotowork
{
NSLog(@"我坐公交车去上班!");
}
-(Teacher*)initWithName:(NSString *)newname andSex:(NSString *)newsex andAge:(int)newage andMarriage:(NSString *)newmarriage
{
self = [super init];
if(self)
{
name = newname;
sex = newsex;
age = newage;
marriage = newmarriage;
}
return self;
}
+(Teacher*)teacherWithName:(NSString *)newname andSex:(NSString *)newsex andAge:(int)newage andMarriage:(NSString *)newmarriage
{
Teacher *tea = [[Teacher alloc]initWithName:newname andSex:newsex andAge:newage andMarriage:newmarriage];
return tea;
}
-(NSString*)description
{
return[NSString stringWithFormat:@"我叫%@是个%@性今年%d目前%@!",name,sex,age,marriage];
}
@implementation Teacher
-(void)sayHi
{
NSLog(@"Goodmorning!");
}
-(void)gotowork
{
NSLog(@"我坐公交车去上班!");
}
-(Teacher*)initWithName:(NSString *)newname andSex:(NSString *)newsex andAge:(int)newage andMarriage:(NSString *)newmarriage
{
self = [super init];
if(self)
{
name = newname;
sex = newsex;
age = newage;
marriage = newmarriage;
}
return self;
}
+(Teacher*)teacherWithName:(NSString *)newname andSex:(NSString *)newsex andAge:(int)newage andMarriage:(NSString *)newmarriage
{
Teacher *tea = [[Teacher alloc]initWithName:newname andSex:newsex andAge:newage andMarriage:newmarriage];
return tea;
}
-(NSString*)description
{
return[NSString stringWithFormat:@"我叫%@是个%@性今年%d目前%@!",name,sex,age,marriage];
}
@end
主程序:
#import
#import "Teacher.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
Teacher *tea = [Teacher teacherWithName:@"xxx" andSex:@"男"andAge:28 andMarriage:@"未婚"];
[tea sayHi];
[tea gotowork];
NSLog(@"%@",tea);
}
return 0;
#import "Teacher.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
Teacher *tea = [Teacher teacherWithName:@"xxx" andSex:@"男"andAge:28 andMarriage:@"未婚"];
[tea sayHi];
[tea gotowork];
NSLog(@"%@",tea);
}
return 0;
}
运行结果:
2015-10-20 21:15:29.776 assignment 1[4336:196173] Goodmorning!
2015-10-20 21:15:29.777 assignment 1[4336:196173] 我坐公交车去上班!
2015-10-20 21:15:29.777 assignment 1[4336:196173] 我坐公交车去上班!
2015-10-20 21:15:29.777 assignment 1[4336:196173] 我叫xxx是个男性今年28目前未婚