我要检查,如果一个子类的以下实现是正确的目标C.超类初始化
首先代码父父写的Objective C的子类:
#import "Employee.h"
@implementation Employee
-(id)init
{
return [self initEmployee:(NSString *)@"" withId:(NSString *)0];
}
-(id)initEmployee:(NSString *)theName withId:(NSString *)theId
{
self = [super init];
if (self) {
_name = theName;
_EmpId = theId;
}
return self;
}
@end
然后子类。
#import "FullTimeEmp.h"
@implementation FullTimeEmp
-(id)init
{
return [self initEmployee:(NSString *)@""
withId:(NSString *)@""
andSalary:(NSNumber *)0
withBonus:(NSNumber *)0];
}
-(id)initEmployee:(NSString *)theName
withId:(NSString *)theId
andSalary:(NSNumber *)theSalary
withBonus:(NSNumber *)theBonus
{
self = [super initEmployee:(NSString *)theName withId:(NSString *)theId];
if (self) {
self.salary = theSalary;
self.bonus = theBonus;
}
return self;
}
@end
这运行正常,但我一直没能确认这是做了正确的方式,使用:
自我= [超级initEmployee:(的NSString *)theName withId:(的NSString * )theId];
在继承超类部分(Name和ID),然后是子类部分的子类中,指定组成Full Time Employee对象的子类添加(Salary和Bonus)。
我读过所有关于我们使用自己的理由= [超级初始化],但已很难找到如何在子类的初始化是在我的书我还是在网上编码很好的例子。
**为什么**哦,为什么所有这些多余的,丑陋的,不可读的,不正确的演员? :((((( – 2014-01-26 21:15:33
请阅读[* Cocoa的编码指南:命名方法*](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html)。Better名字是'initWithName:employeeId:'和'initWithName:employeeId:salary:bonus:'。 –