如何将超类属性复制到子类?

发布于 2025-01-02 21:10:29 字数 438 浏览 4 评论 0原文

假设我有两个类,如下所示:

Car
{
   NSInteger wheels;
   NSInteger bumpers;
}

+ (Car *)carWithData:(NSDictionary *)carData;


Lexus : Car
{
   GPS *navigation;
}

+ (Lexus *)carWithData:(NSDictionary *)carData;

carWithData: 是一个简单的辅助方法,它创建一个 Car 实例,并用 carData 中的变量填充。雷克萨斯的版本还会设置导航数据。

如果不复制 Car 的代码,雷克萨斯的 carWithData 会是什么样子?

Let's say I have two Classes like so:

Car
{
   NSInteger wheels;
   NSInteger bumpers;
}

+ (Car *)carWithData:(NSDictionary *)carData;


Lexus : Car
{
   GPS *navigation;
}

+ (Lexus *)carWithData:(NSDictionary *)carData;

carWithData: is a simple helper method that creates an instance of Car populated with variables from carData. Lexus' version would also set the navigation data.

How would Lexus' carWithData look like without duplicating code from Car?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自控 2025-01-09 21:10:30

这是通过在 init 方法中调用 superinit... 实现来完成的:

//Car.m:
- (id)initWithData:(NSDictionary *)carData {
    self = [super init];
    if (self) {
        //setup generic car properties:
        self.wheels = [carData objectForKey:@"wheels"]; //example
        self.bumpers = [carData objectForKey:@"bumpers"]; //example
    }
    return self;
}

+ (id)carWithData:(NSDictionary *)carData {
    return [[[self alloc] initWithData:carData] autorelease];
}


//Lexus.m:
- (id)initWithData:(NSDictionary *)carData {
    //this call to super is where the car's generic properties get initialized:
    self = [super initWithWithData:carData];
    if (self) {
        //setup lexus car properties:
        self.navigation = [carData objectForKey:@"navigation"]; //example
    }
    return self;
}

//there is no need to override super's [carWithData:] method as it's only a wrapper anyway.

另请注意,initWith...carWith ... 方法返回 id,而不是 CarLexus
您的代码设置方式最终会出现转换问题,其中 [Lexus carWithData:dataDict] 确实返回类 Lexus 的对象,但编译器不知道关于它,因为它需要一辆汽车

This is accomplished by calling super's implementation of init… in the init method:

//Car.m:
- (id)initWithData:(NSDictionary *)carData {
    self = [super init];
    if (self) {
        //setup generic car properties:
        self.wheels = [carData objectForKey:@"wheels"]; //example
        self.bumpers = [carData objectForKey:@"bumpers"]; //example
    }
    return self;
}

+ (id)carWithData:(NSDictionary *)carData {
    return [[[self alloc] initWithData:carData] autorelease];
}


//Lexus.m:
- (id)initWithData:(NSDictionary *)carData {
    //this call to super is where the car's generic properties get initialized:
    self = [super initWithWithData:carData];
    if (self) {
        //setup lexus car properties:
        self.navigation = [carData objectForKey:@"navigation"]; //example
    }
    return self;
}

//there is no need to override super's [carWithData:] method as it's only a wrapper anyway.

Also note that both the initWith… and carWith… methods return id, not Car or Lexus.
The way your code is set up you end up with casting problems, where [Lexus carWithData:dataDict] does return an object of class Lexus, but the compiler doesn't know about it, as it expects a Car.

卷耳 2025-01-09 21:10:30

您不会使用不同的签名来定义方法,例如:

+ (Car *)carWithData:(NSDictionary *)carData;
+ (Lexus *)carWithData:(NSDictionary *)carData;

您应该使用

+ (id)carWithData:(NSDictionary *)carData;

子类的实现将如下所示

- (id)initWithData:(NSDictionary *)carData;
{
    self = [super initWithData:carData];
    if (self) {
         _navigation = [carData valueForKey:@"navigation"];
    }
    return self;
}

+ (id)carWithData:(NSDictionary *)carData;
{
    return [[[self alloc] initWithCarData:carData] autorelease];
}

You would not define the methods with different signatures like:

+ (Car *)carWithData:(NSDictionary *)carData;
+ (Lexus *)carWithData:(NSDictionary *)carData;

you should instead use

+ (id)carWithData:(NSDictionary *)carData;

The implementation of the subclass would then look like

- (id)initWithData:(NSDictionary *)carData;
{
    self = [super initWithData:carData];
    if (self) {
         _navigation = [carData valueForKey:@"navigation"];
    }
    return self;
}

+ (id)carWithData:(NSDictionary *)carData;
{
    return [[[self alloc] initWithCarData:carData] autorelease];
}
情场扛把子 2025-01-09 21:10:30

这是我的解决方案:

// interface
-(id) initWithCarData:(NSDictionary *) carData;
+(Car *) carWithCarData:(NSDictionary *) carData;

// car implementation
-(id) initWithCarData:(NSDictionary *) carData
{
    if (self = [super init])
    {
          // initialize car data
    }

    return self;
}

+(Car *) carWithCarData:(NSDictionary *) carData
{
    // note that 'self' here is the current class, 
    // there is no need to overwrite this method in the subclass
    return [[self alloc] initWithCarData:carData];
}

// lexus implementation
-(id) initWithCarData:(NSDictionary *) carData
{
     // initialize the variables that the superclass recognizes
     if (self = [super initWithCarData:carData])
     {
         // initialize the lexus data
     }

     return self;
}

因此,当您调用 [Lexus carWithCarData:myData] 时,它最终会调用 Lexusinit 方法,而不是汽车的。

Here would be my solution:

// interface
-(id) initWithCarData:(NSDictionary *) carData;
+(Car *) carWithCarData:(NSDictionary *) carData;

// car implementation
-(id) initWithCarData:(NSDictionary *) carData
{
    if (self = [super init])
    {
          // initialize car data
    }

    return self;
}

+(Car *) carWithCarData:(NSDictionary *) carData
{
    // note that 'self' here is the current class, 
    // there is no need to overwrite this method in the subclass
    return [[self alloc] initWithCarData:carData];
}

// lexus implementation
-(id) initWithCarData:(NSDictionary *) carData
{
     // initialize the variables that the superclass recognizes
     if (self = [super initWithCarData:carData])
     {
         // initialize the lexus data
     }

     return self;
}

So, when you call [Lexus carWithCarData:myData] it ends up calling the Lexus's init method, not the Car's.

许你一世情深 2025-01-09 21:10:30

还有另一种更通用的方法,即使用 NSCoding 协议和 NSKeyedArchiver。

如果您想要复制到子类中的对象实现了 NSCoding 协议(许多基于 NS... 的类都是这种情况),则可以使用以下完全合法且安全的技巧:

// Assumptions:
// copyFrom is an object of ClassA
// We have a ClassB that is a subclass of ClassA
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[copyFrom encodeWithCoder:arch]; // this archives its properties
[arch finishEncoding];
NSKeyedUnarchiver *ua = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
ClassB *ourCopy = [[ClassB alloc] initWithCoder:ua]; // this restores the properties into our new object

There's another way, which is more generic, by using the NSCoding protocol and NSKeyedArchiver.

If the object you want to copy into a subclass of yours implements the NSCoding protocol, which is the case for many NS... based classes, the following totally legal and safe trick can be used:

// Assumptions:
// copyFrom is an object of ClassA
// We have a ClassB that is a subclass of ClassA
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[copyFrom encodeWithCoder:arch]; // this archives its properties
[arch finishEncoding];
NSKeyedUnarchiver *ua = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
ClassB *ourCopy = [[ClassB alloc] initWithCoder:ua]; // this restores the properties into our new object
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文