对象接收和返回值吗?
我的书提出了这样一个问题:“对象和函数之间有什么区别?”
答案是,函数是按名称引用的接收和返回值的语句集。它说对象可以有多种功能,称为方法;和多个变量,称为属性;全部组合在一个单元中。
不幸的是,这本书没有说明对象是否接收和返回值……那么,是吗?我正在尝试寻找一个例子。
My book has asked the question: "What is the difference between objects and functions?"
The answer key says that functions are sets of statements referenced by name that receive and return values. It says that objects may have multiple functions, called methods; and multiple variables, called properties; all combined in a single unit.
Unfortunately, the book doesn't say whether or not objects receive and return values.... So, do they? I'm trying to find an example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,可以将对象设置为可调用。
但并非所有语言都支持这样的概念,而且这也不是常态。
我查看了您最近提出的一些其他问题。请注意,您不能执行此操作:
这没有任何意义,因为
Foo
在此示例中是一个类。请记住,术语“对象”指的是“类”的特定实例(例如,
new Foo()
)。Yes, an object can be set up to be callable.
But not all languages support such a concept, and it's not the norm.
I took a look at some of the other questions you have asked recently. Note that you cannot do this:
That does not make any sense since
Foo
is a class in this example.Keep in mind that the term "object" refers to a specific instance (e.g.,
new Foo()
) of a "class."对象的方法是函数,并且在大多数情况下,其工作方式与常规函数一样。不同之处在于,方法具有不同的可见性,并且可以访问其对象的属性,而无需将其传入。
An object's methods are functions and, for the most part, work just like regular functions. The differences are that methods have different visibilities and have access to their object's properties without having to have them passed in.