objective-c初始化函数的问题
#import"Tire.h"@implementationTire-(id)initWithPressure:(float)ptreadDepth:(float)td{...
#import "Tire.h"
@implementation Tire
- (id) initWithPressure: (float) p
treadDepth: (float) td
{
if (self = [super init]) {
pressure = p;
treadDepth = td;
}
return (self);
} // initWithPressure:treadDepth:
- (id) init
{
if (self = [self initWithPressure: 34
treadDepth: 20]) {
}
return (self);
} // init
- (id) initWithPressure: (float) p
{
if (self = [self initWithPressure: p
treadDepth: 20.0]) {
}
return (self);
} // initWithPressure
- (id) initWithTreadDepth: (float) td
{
if (self = [self initWithPressure: 34.0
treadDepth: td]) {
}
return (self);
} // initWithTreadDepth
- (void) setPressure: (float) p
{
pressure = p;
} // setPressure
- (float) pressure
{
return (pressure);
} // pressure
- (void) setTreadDepth: (float) td
{
treadDepth = td;
} // setTreadDepth
- (float) treadDepth
{
return (treadDepth);
} // treadDepth
- (NSString *) description
{
NSString *desc;
desc = [NSString stringWithFormat:
@"Tire: Pressure: %.1f TreadDepth: %.1f",
pressure, treadDepth];
return (desc);
} // description
@end // Tire
我很好奇,- (id) initWithPressure: (float) p - (id) initWithTreadDepth: (float) td 两个初始化函数其实删了好像也没什么影响,因为感觉
- (id) initWithPressure: (float) p
treadDepth: (float) td
把它们的工作都做了,它们的作用到底是什么呢或者是可以删掉的。 展开
@implementation Tire
- (id) initWithPressure: (float) p
treadDepth: (float) td
{
if (self = [super init]) {
pressure = p;
treadDepth = td;
}
return (self);
} // initWithPressure:treadDepth:
- (id) init
{
if (self = [self initWithPressure: 34
treadDepth: 20]) {
}
return (self);
} // init
- (id) initWithPressure: (float) p
{
if (self = [self initWithPressure: p
treadDepth: 20.0]) {
}
return (self);
} // initWithPressure
- (id) initWithTreadDepth: (float) td
{
if (self = [self initWithPressure: 34.0
treadDepth: td]) {
}
return (self);
} // initWithTreadDepth
- (void) setPressure: (float) p
{
pressure = p;
} // setPressure
- (float) pressure
{
return (pressure);
} // pressure
- (void) setTreadDepth: (float) td
{
treadDepth = td;
} // setTreadDepth
- (float) treadDepth
{
return (treadDepth);
} // treadDepth
- (NSString *) description
{
NSString *desc;
desc = [NSString stringWithFormat:
@"Tire: Pressure: %.1f TreadDepth: %.1f",
pressure, treadDepth];
return (desc);
} // description
@end // Tire
我很好奇,- (id) initWithPressure: (float) p - (id) initWithTreadDepth: (float) td 两个初始化函数其实删了好像也没什么影响,因为感觉
- (id) initWithPressure: (float) p
treadDepth: (float) td
把它们的工作都做了,它们的作用到底是什么呢或者是可以删掉的。 展开
1个回答
2014-11-06
展开全部
只不过是简便写法而已
例如有了initWithTreadDepth方法就可以写
[[Tire alloc] initWithTreadDepth:42.0]
而不用写成
[[Tire alloc] initWithPressure:34.0 treadDepth:42.0]
后者写多了会很烦的
追问
用前者初始化的话,pressure是会被初始化为34.0还是0呢?
懂了。。谢了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询