xcode 什么是Objective-C中的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6804349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is an object in Objective-C?
提问by Instafal
I thought I had a pretty good idea of what an object is.. but I was wrong. Could anyone explain what an object is? Or how I should think of it when programming? Please help me understand.
我以为我很清楚什么是对象……但我错了。谁能解释一下什么是对象?或者我在编程时应该怎么想?请帮我理解。
I know it's not the pointer .. so what exactly is the object in a line of code ..
我知道这不是指针 .. 那么一行代码中的对象究竟是什么..
回答by Martin Pilkington
Conceptually in OOP, an object is a certain instance of a class. A class defines the information and actions for a certain type of object. The quintessential example is of a Car class, that maybe holds a "colour" property and can "drive" or "park". These define what a Car is. Objects are instances of that. There are millions of cars in the world, but only one definition of Car
从概念上讲,在 OOP 中,对象是类的某个实例。类定义了特定类型对象的信息和操作。典型的例子是 Car 类,它可能拥有“颜色”属性并且可以“开车”或“停车”。这些定义了汽车是什么。对象是它的实例。世界上有数百万辆汽车,但汽车只有一种定义
Now, that is the general OOP view. Objective-C has a rather pure OOP model, based on the concept of message sending. Message sending is performed as so: [obj message]. Essentially, an object in Obj-C is anything that responds to a message. This means that even classes are objects. If you want a more detailed description, this blog post of mine should help: http://pilky.me/view/21
现在,这是一般的 OOP 视图。Objective-C 有一个相当纯的 OOP 模型,基于消息发送的概念。消息发送是这样执行的:[obj message]。本质上,Obj-C 中的对象是响应消息的任何东西。这意味着即使是类也是对象。如果您想要更详细的描述,我的这篇博文应该会有所帮助:http: //pilky.me/view/21
回答by Dave
An object is an instance of a class. So, suppose you have a class person. You might initialize him somewhere:
对象是类的实例。所以,假设你有一个班级的人。你可以在某处初始化他:
Person *p = [[Person alloc] init];
p is a pointer to a person object in memory. The object itself lives in the sizeof(Person) chunk of ram which alloc created.
p 是指向内存中的 person 对象的指针。对象本身存在于 alloc 创建的 ram 的 sizeof(Person) 块中。
回答by Erre Efe
Basically objects are data plus functions grouped. So you have state (data aka fields, variables) and behavior (functions). The pointer is how you reference the object (in order to use it later, maybe to retrieve its data, execute a function or send a message).
基本上,对象是分组的数据加函数。所以你有状态(数据又名字段、变量)和行为(函数)。指针是您引用对象的方式(为了稍后使用它,可能是检索其数据、执行函数或发送消息)。
Here you have a nice and simple explanation: http://gnustep.made-it.com/BG-objc/#AEN281
在这里,您有一个很好而简单的解释:http: //gnustep.made-it.com/BG-objc/#AEN281
回答by EdoDodo
Check out this tutorial about the basics of Objective-C:
查看本教程,了解 Objective-C 的基础知识:
http://cocoadevcentral.com/d/learn_objectivec/
http://cocoadevcentral.com/d/learn_objectivec/
If you still have questions, feel free to ask.
如果您仍有疑问,请随时提问。
回答by Nathanial Woolls
I'll start by saying, having used a few other programming languages, the concept of an object in Objective-C is pretty much the same as it is in other Object Oriented Programming languages. There's a good writeup on Wikipedia.
我首先要说的是,在使用了一些其他编程语言之后,Objective-C 中对象的概念与其他面向对象编程语言中的概念非常相似。维基百科上有一篇很好的文章。
I'd say an easy way to think about it is that a "class" is a blueprint. It describes how a thing should work. An "object" is an actual instance of a thing that follows that blueprint.
我想说一个简单的思考方式是“类”是一个蓝图。它描述了事物应该如何工作。“对象”是遵循该蓝图的事物的实际实例。
You are building a house (program). You need to drive nails. You create an instance of a hammer "object" following the blueprint described by the "class" definition. Now you can hammer nails.
你正在盖房子(程序)。你需要钉钉子。您按照“类”定义描述的蓝图创建锤子“对象”的实例。现在你可以钉钉子了。
A class generally has an interface and an implementation. This allows you to (and others) to call the code on your object having only to (generally) look at the interface.
一个类通常有一个接口和一个实现。这使您(和其他人)只需(通常)查看界面即可调用对象上的代码。