向对象发送消息?

发布于 2024-11-01 05:59:10 字数 422 浏览 1 评论 0原文

我以前已经成功地做过很多次了,但这次就是不行。

我的 .h 文件中的代码:(

@interface Images : NSView {

}

- (void) draw;

@end

另一个对象的).m 文件:

- (IBAction) go: (id) sender; {
[Images draw:nil];

}

当我尝试编译此文件时,它显示以下内容:

'*' 可能不会响应 '*

Images可能不会响应“+draw”

这让我很困惑。这不应该起作用吗?

请注意:是的,我看过有关消息的其他问题,但这些答案都对我没有部分帮助。读完之后,我更加困惑了。

I have done this successfully many times before, but this time it just won't work.

The code in my .h file:

@interface Images : NSView {

}

- (void) draw;

@end

The .m file (of another object):

- (IBAction) go: (id) sender; {
[Images draw:nil];

}

When I try to compile this, it says the following:

'*' may not respond to '*

Images may not respond to '+draw'

This has me quite confused. Shouldn't this be working?

Please Note: Yes, I have seen the other questions about messages, but none of those answers was even partially helpful for me. After reading them, I was even more confused.

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

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

发布评论

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

评论(2

送你一个梦 2024-11-08 05:59:10

您的 draw 方法是一个实例方法:只能在 Images 类的实例上调用它。在您的 go: 方法中,您尝试将其作为类方法调用 - 如果这是您想要的,请将其更改为:

@interface Images : NSView
+ (void)draw;
@end

Your draw method is an instance method: it can only be called on instances of the Images class. In your go: method you're trying to call it as a class method—if this is what you want, change it to:

@interface Images : NSView
+ (void)draw;
@end
悍妇囚夫 2024-11-08 05:59:10

我认为回顾一下面向对象编程的一些基本概念是有必要的;即类与对象或实例之间的区别。

一般意义上,类是数据和作用于数据的函数的集合。类定义了用于访问和操作逻辑上分组在一起的数据的接口,并充当创建对象或实例的蓝图。请参阅 http://en.wikipedia.org/wiki/Class_(computer_programming)

实例类(对象)是您在面向对象程序中操作的典型事物,它们是根据类“蓝图”创建的,并遵循类指定的行为。

一个典型的例子是水果,例如苹果。 的 Apple 类将代表一般意义上的所有苹果,并对颜色、大小以及清洗和食用等动作进行建模。一个实例将代表一个单一的物理苹果——Granny Smith 或 Pippin 或任何品种。

一个虚构 因为在这个词的一般意义上洗或吃苹果是没有意义的(苹果的概念,而不是结块),通常告诉一个类你告诉对象(个体)做什么是没有意义的。 apples)要做什么。

上面的代码定义了 Images 类。 -(void)draw 前面的“-”表示draw方法仅适用于特定对象。它是典型 OO 术语中的实例方法。

当然,在 Obj-C 中,也可以在不需要对象的情况下向类发送消息。正如其他答案所示,这由方法名称前面的“+”表示。这称为静态方法,它通常用于控制该特定类的所有对象的某些共享行为或方面。

您的代码的问题在于您将 -(void)draw 声明为实例方法,但将其作为静态方法调用。您想要以哪种方式做事取决于您,并且很难从代码中确定 Images 类的意图是什么。

I think a review of some of the basic concepts of object-oriented programming is in order; namely the difference between a class and an object or instance.

A class, in the general sense, is a collection of data and the functions which act upon it. A class defines the interface that one uses to access and manipulate data that is logically grouped together, and serves as a blueprint for creating objects or instances. See http://en.wikipedia.org/wiki/Class_(computer_programming)

Instances of a class (objects) are the typical things you manipulate in an object-oriented program, and they are created from the class "blueprint" and follow the behavior as specified by the class.

A typical example would be a fruit- take apples for example. An imaginary Apple class would represent all apples in the general sense and would model properties such as color and size and actions such as wash and eat. An instance would represent one, single physical apple- a Granny Smith or Pippin or whatever variety.

Just as it doesn't make sense to wash or eat apples in the general sense of the word (the concept of apples, not the agglomeration), typically it doesn't make sense to tell a class what to do. You tell objects (individual apples) what to do.

The code you present above defines the class Images. The "-" in front of -(void)draw indicates that the draw method only exists for specific objects. It is an instance method in typical OO parlance.

Of course, in Obj-C it is also possible to send a message to a class without requiring an object. This is denoted by a "+" in front of the method name, as other answers indicate. This is called a static method and it typically used to control some shared behavior or aspect of all objects of that particular class.

The problem with your code is that you are declaring -(void)draw as an instance method but calling it as a static method. Which way you want to do things is up to you, and it's difficult to determine from your code what the intent of the Images class is.

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