What is an object in Objective-C?

给你一囗甜甜゛ 提交于 2020-01-04 06:15:08

问题


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 ..


回答1:


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

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




回答2:


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.




回答3:


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




回答4:


Check out this tutorial about the basics of Objective-C:

http://cocoadevcentral.com/d/learn_objectivec/

If you still have questions, feel free to ask.




回答5:


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.

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.



来源:https://stackoverflow.com/questions/6804349/what-is-an-object-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!