Objective-C 类的继承和分配

发布于 2024-11-04 09:44:23 字数 431 浏览 4 评论 0原文

给出下面的行,其中 Square 是 Rectangle 的子类:

Rectangle *thisObject = [[Square alloc] init];

thisObject 包含: 1. Square对象的所有实例变量。 2. Square对象中实现的所有实例方法。 3. 矩形对象的所有实例变量。 4. 矩形对象中实现的所有实例方法。

给出下面的行,其中 Square 是 Rectangle 的子类:

Square *thisObject = [[Square alloc] init];

thisObject 包含: 1. Square对象的所有实例变量。 2. Square对象中实现的所有实例方法。 3. 矩形对象中实现的所有实例方法。

有什么不同意见吗?

Given the line below where Square is a subclass of Rectangle:

Rectangle *thisObject = [[Square alloc] init];

thisObject contains:
1. All the instance variables of a Square object.
2. All the instance methods implemented in the Square object.
3. All the instance variables of a Rectangle object.
4. All the instance methods implemented in the Rectangle object.

Given the line below where Square is a subclass of Rectangle:

Square *thisObject = [[Square alloc] init];

thisObject contains:
1. All the instance variables of a Square object.
2. All the instance methods implemented in the Square object.
3. All the instance methods implemented in the Rectangle object.

Any disagreements?

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

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

发布评论

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

评论(3

人间☆小暴躁 2024-11-11 09:44:26

我不同意。您错过了第二个还包含 Rectangle 对象的所有实例变量。

两个实例完全相同。除了类簇之类的东西之外,实例是您将它们分配的内容,而不是您后来声称的内容。

这两个实例都是 Square 对象。

这条线:

Monkey *george = [[Square alloc] init];

没有做任何事情让乔治成为猴子。他是一个正方形。如果您向他发送 Monkey 不响应的消息,编译器会警告您,但如果它们是 Square 消息(或 Square 的任何超类),他将作为 Square 进行响应。

如果您发送 george Monkey 消息,您将收到有关选择器未找到的运行时错误。除非您的 Monkey 消息恰好与 Square 消息匹配(这将在其公共超类中的方法中发生)。

这就是多态性。你可以声称乔治是一只猴子,或者一个圆圈,或者只是一个ID,但他会回答他是什么,一个正方形。

I disagree. You missed that the second also contains all the instance variables of a Rectangle object.

Both instances are exactly the same. Instances are, with the exception of things like class clusters, what you alloc them as, not what you later claim them to be.

Both of those instances are of Square objects.

The line:

Monkey *george = [[Square alloc] init];

does not do anything to make george a Monkey. He's a Square. The compiler will warn you if you send him messages that Monkey doesn't respond to, but if they're Square messages (or any superclass of Square), he'll respond as a Square.

And if you send george Monkey messages, you'll get runtime errors about selector not found. Unless your Monkey message just happens to match a Square message (which will happen with methods in their common superclasses).

That's polymorphism. You can claim george is a Monkey, or a Circle, or just an id, but he'll respond as what he is, a Square.

泅渡 2024-11-11 09:44:26

你是对的。指针的类型与对象本身的类型无关。它只是使编译器更容易检查类型。您还可以对所有对象使用id,并且它们的行为不会改变。

You're correct. The type of the pointer doesn't matter for the type of the object itself. It just makes it easier for the compiler to check the types. You could also use id for all of your objects and their behavior wouldn't change.

北音执念 2024-11-11 09:44:25

不,这些是相同的。两者之间的唯一区别是,如果您尝试向键入为 Rectangle 的 thisObject 发送 Square 消息,您将会收到编译器发出的警告。但这只是一个警告,消息发送实际上会在运行时起作用。

No, these are identical. The only difference between the two is that you will get a warning from the compiler if you tried to send a Square message to the thisObject typed as a Rectangle. But this will only be a warning, the message send would actually work at runtime.

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