对象,类,消息 6 [完]

本文详细阐述了类名在源代码中作为数据类型和对象的双重角色,并介绍了如何正确测试类对象的相等性,强调了在使用类名时避免全局变量冲突的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Class Names in Source Code

在代码中,类名只能出现在两个不同的上下文中,这两个上下文反应了类名可以作为数据类型也对象(类对象)的角色.

In source code, class names can be used in only two very different contexts. These contexts reflect the dual  role of a class as a data type and as an object:

● The class name can be used as a type name for a kind of object. For example:

Rectangle *anObject; ///1.作为数据类型.标志某种对象.

Here anObject is statically typed to be a pointer to a Rectangle object. The compiler expects it to have the data structure of a Rectangle instance and to have the instance methods defined and inherited by  the Rectangle class. Static typing enables the compiler to do better type checking and makes source  code more self-documenting.

Only instances can be statically typed; class objects can’t be, because they aren’t members of a class, but  rather belong to the Class data type.

//只有实例对象才能使用静态类型确定,类对象是一种 Class 的数据类型,不能使用静态类型确定

● As the receiver in a message expression, the class name refers to the class object. The class name can stand for the class object only as a message receiver.
In any other context, you must ask the class object to reveal its id (by sending it a class message). This不当example passes the Rectangle class as a parameter in an isKindOfClass: message:


if ( [anObject isKindOfClass:[Rectangle class]] ) //2.作为消息接收者

...


It would have been illegal to simply use the name “Rectangle” as the parameter. The class name can only be a receiver.
If you don’t know the class name at compile time but have it as a string at runtime, you can use  NSClassFromString to return the class object:


NSString *className;
...
if ( [anObject isKindOfClass:NSClassFromString(className)] ) //如果类名是字符,,,,,
...
This function returns nil if the string it’s passed is not a valid class name.


Class names exist in the same namespace as global variables and function names. A class and a global variable  can’t have the same name. Class names are the only names with global visibility in Objective-C.

全局变量不能与类名相同,在Object C 中类名是唯一具有全局可见性的.


Testing Class Equality

得到正确的类是非常重要的,这可以通过指针比较两个类对象是否相同,在Cocoa framework 有些特性可以动态地透明地子类化现有的类,以扩展他们的功能,如:ey-value observing and Core Data 


You can test two class objects for equality using a direct pointer comparison. It is important, though, to get the correct class. There are several features in the Cocoa frameworks that dynamically and transparently subclass  existing classes to extend their functionality (for example, key-value observing and Core Data do this—see
Key-Value Observing Programming Guide and Core Data Programming Guide respectively).

在动态扩展子类通常覆盖原来的类的方法,或者说假扮成原来的类的方法,当做类相等测试时候,就要通过测试class 方法的返回值,

In a  dynamically-created subclass, the class method is typically overridden such that the subclass masquerades (假扮;乔装)  as the class it replaces. When testing for class equality, you should therefore compare the values returned by  the class method rather than those returned by lower-level functions. Put in terms of API, the following
inequalities pertain for dynamic subclasses:


[object class] != object_getClass(object) != *((Class*)object)


You should therefore test two classes for equality as follows:
if ([objectA class] == [objectB class]) { //...需要这样测试类是否相同










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值