그 자식 클래스에서 MyDerivedClass
이니셜 라이저. 여기
[MyDerivedClass initByCopying:someMyObject plusSomeNewProperties:stuff] ->
[MyClass initByCopying:someMyObject] ->
[NSObject init] -> // alloc, etc.
어떤 의사이다 : 나는 1000 개 멤버 항목이있는 경우 당신이 만드는 것이다, 성능에 민감한 사람하지만, 모든 속성 가방이나 KVC 사용을 고려할 수 있다는 의심
@interface MyClass : NSObject {
int AA;
// ...
int ZZ;
}
@end
@implementation MyClass
-initByCopying:(MyClass*)other;
{
if (self = [super init])
{
self.AA=other.AA;
//...
self.ZZ=other.ZZ;
}
return self;
}
@end
@interface MyDerivedClass {
int AAA;
}
@end
@implementation MyDerivedClass
-initByCopying:(MyClass*)other withNewValue:(int)newVar;
{
if (self = [super initByCopying:(MyClass*)other])
{
self.AAA = newVar;
}
return self;
}
@end
당신의 initByCopying
루틴은 훨씬 간단합니다.
복사 프로토콜을 사용하여 복사 생성자를 구현하기위한 바로 가기가있을 수 있지만 위의 예보다 쉽게 만드는 방법을 볼 수 없습니다.
하지만 질문에 대한 오해가있을 수 있습니다. 하위 클래스의 ivars를 덮어 쓰시겠습니까? –
아니요, 아무 것도 무시하고 싶지 않습니다. –