Objective-C中是类对象吗? [英] Are classes objects in Objective-C?

查看:99
本文介绍了Objective-C中是类对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我明白一个对象是一个类的实例必须被分配和初始化,但是类本身的对象?

okay, so I understand that an object is an instance of a class that must be allocated and initialized, but are classes themselves objects?

我知道当你创建一个新类它是一个别的东西的实例,如NSObject。所以,如果这使它成为一个类,那么对象不仅可以保存变量和方法,还可以保存其他对象,对吗?

I know when you create a new class it is an instance of something else, like NSObject. So, if this makes it a class, then objects can hold not only variables and methods, but other objects as well, right?

对不起,这可能是基本的,但我读两本关于可可和xcode的书,这一点有点不清楚(可能是因为我缺乏其他语言的经验)。

Sorry, this is probably really basic, but I am reading two books about cocoa and xcode and this point is a little unclear (probably because of my lack of experience in other languages).

推荐答案

这里是对Greg的一个很好的解释Parker

Here is a pretty good explanation of the matter by Greg Parker

引用:


[...]也是一个
对象。它有一个isa指针和
其他数据,并可以响应
选择器。当你调用一个class
method像[NSObject alloc],你是
实际上发送消息到
类对象。

[...] Each Objective-C class is also an object. It has an isa pointer and other data, and can respond to selectors. When you call a "class method" like [NSObject alloc], you are actually sending a message to that class object.

由于一个类是一个对象,它必须是
一个其他类的实例:a
metaclass。元类是类对象的
描述,只是
像类一样是对
的普通实例的描述。特别地,
元类的方法列表是类
方法:类
对象响应的选择器。当你向一个类发送
消息时 - 一个
元类的实例--objc_msgSend()通过
元类的方法列表(及其超类,如果$ b)看起来是
$ b any)来决定调用什么方法。
类方法由
元类描述,代表类
对象,就像实例方法是
,由类描述的代表
的实例对象。

Since a class is an object, it must be an instance of some other class: a metaclass. The metaclass is the description of the class object, just like the class is the description of ordinary instances. In particular, the metaclass's method list is the class methods: the selectors that the class object responds to. When you send a message to a class - an instance of a metaclass - objc_msgSend() looks through the method list of the metaclass (and its superclasses, if any) to decide what method to call. Class methods are described by the metaclass on behalf of the class object, just like instance methods are described by the class on behalf of the instance objects.

元类怎么样?是
元类一路下来吗? A
元类是根
类的元类的一个实例;根元类
本身是根
元类的实例。 isa链以
循环结束:instance to class to
metaclass to root metaclass to itself。
元类isa指针的行为
很少重要,因为在真实的
世界中没人发送消息到
元类对象。 [...]

What about the metaclass? Is it metaclasses all the way down? No. A metaclass is an instance of the root class's metaclass; the root metaclass is itself an instance of the root metaclass. The isa chain ends in a cycle here: instance to class to metaclass to root metaclass to itself. The behavior of metaclass isa pointers rarely matters, since in the real world nobody sends messages to metaclass objects. [...]

更有趣的是:

Colin Wheeler了解Objective-C运行时

(搜索标题为So Classes define objects ...的段落)

Understanding the Objective-C Runtime by Colin Wheeler
(search for paragraph titled "So Classes define objects…")

什么是Objective-C中的元类?by Matt Gallagher

What is a meta-class in Objective-C? by Matt Gallagher

这篇关于Objective-C中是类对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆