Objective C——初始化子类对象的继承变量

发布于 2024-08-17 18:02:18 字数 297 浏览 4 评论 0原文

我有一个班级MyClass。我在这里有点夸张,但假设 MyClass 有 1000 个实例变量。然后,我创建一个名为 MySubClass 的子类,其中包含 MyClass 拥有的所有实例变量,再加上一个。

问题:给定 MyClass 类的对象 MyObj,是否有一种简单的方法来创建 MySubClass 类的对应对象 MyDerivedObj,使得 MyDerivedObj 的实例变量与 MyObj 的实例变量相同?我所说的“相同”是指强烈相同,即如果 MyObj 的实例变量是指向对象的指针,则 MyDerivedObj 的相应实例变量应该指向相同的内存。

I have a class MyClass. I am exaggerating here, but let's say MyClass has 1000 instance variables. I then create a subclass called MySubClass with all the instance variables MyClass has, plus one more.

Question: given an object MyObj of class MyClass, is there an easy way to create a corresponding object MyDerivedObj of class MySubClass, such that the instance variables of MyDerivedObj are the same as the instance variables of MyObj? By "the same", I mean strongly the same, in the sense that if an instance variable of MyObj is a pointer to an object, the corresponding instance variable of MyDerivedObj should point to the same memory.

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

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

发布评论

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

评论(4

独﹏钓一江月 2024-08-24 18:02:18

本质上,对象的每个实例都会有不同的 id;堆中不同的地址和不同的分配点。

因此,A 的实例变量和 B 的实例变量总是位于不同的位置

现在,A 和 B 的实例变量没有理由不能包装到单独分配的结构中。这样,A 和 B 都可以拥有一个实例变量,该变量是指向充满值的结构的单个副本的指针。

至于设置所有 1,000 个 ivars,这也取决于您想要设置它们。如果为 0,则它们将在对象实例化时自动设置为这种方式。如果您想在模板化的值集中使用bcopy(),我建议您使用指向结构的指针并进行单独的分配。没有办法批量设置对象的实例变量而不对布局做出最终会咬你的假设。

Inherently, every instance of an object will have a different id; a different address and a different allocation point in the heap.

Thus, the instance variables of A and the instance variables of B are always going to be at different locations.

Now, there is no reason why the instance variables of A and B can't be wrapped into a struct that is allocated separately. With that, then A and B could both have an instance variable that is a pointer to a single copy of a structure full of values.

In terms of setting all 1,000 ivars, it depends on what you want to set them too. If 0, then they will be set that way automatically on object instantiation. If you want to bcopy() in a templated set of values, I would suggest that you use a pointer to a structure and do a separate allocation. There is no way to bulk-set an object's instance variables without making assumptions about layout that will eventually bite you.

你是年少的欢喜 2024-08-24 18:02:18

这些 ivars 都必须分开吗?如果我遇到类似的问题,我的第一反应是将它们包装在某种集合 ivar (NS(Mutable)Array/Dictionary/Set) 中,然后你可以在上面有一个普通的 getter/setter 并

myDerivedObj.collection = myObj.collection;

假设集合是 MyObj 类上具有“分配”内存管理策略的属性,我认为这应该保留内存引用。

(我对此还很陌生,所以请消除我逻辑中的任何缺陷/错误。)

Do those ivars all have to be separate? If I had a similar problem, my first instinct would be to wrap them up in some sort of collection ivar (NS(Mutable)Array/Dictionary/Set) and then you can have a normal getter/setter on it and just do

myDerivedObj.collection = myObj.collection;

Assuming the collection was a property on MyObj class with "assign" memory management policy, I think this should preserve the memory reference.

(I'm still kind of new to this, so shoot down any flaws/errors in my logic.)

用心笑 2024-08-24 18:02:18

如果 ivars 被标记为 @public 或 @protected,是的,它们将完全相同。

It the ivars are marked as @public or @protected, yes, they will be exactly the same.

掌心的温暖 2024-08-24 18:02:18

我建议您为父类 MyClass 创建一个“复制构造函数”样式的初始化程序,并从子类 MyDerivedClass 初始化程序中调用它。

[MyDerivedClass initByCopying:someMyObject plusSomeNewProperties:stuff] ->
  [MyClass initByCopying:someMyObject] ->
    [NSObject init] -> // alloc, etc.

这里有一些伪代码:

@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

我怀疑如果您有 1000 个成员项目,您可能需要考虑对除性能敏感的项目之外的所有项目使用属性包或 kvc,这将使您的 initByCopying 例程更加简单。

可能有一个使用复制协议实现复制构造函数的快捷方式,但我不知道如何使它比上面给出的示例更容易。

I suggest you create a 'copy constructor' style initialiser for the parent class MyClass, and invoke that from the child class MyDerivedClass initialiser.

[MyDerivedClass initByCopying:someMyObject plusSomeNewProperties:stuff] ->
  [MyClass initByCopying:someMyObject] ->
    [NSObject init] -> // alloc, etc.

Here's some pseudocode:

@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

I suspect that if you have 1000 member items you might want to consider using a property bag or kvc for all but the performance sensitive ones, which will make your initByCopying routine much simpler.

There may be a shortcut to implementing the copy constructor using the copy protocol, but I couldn't see how to make it easier than the example I gave above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文