对象,类,消息 4
来源:互联网 发布:如何删除数据库 编辑:程序博客网 时间:2024/05/22 09:41
A class definition is a specification for a kind of object. The class, in effect, defines a data type. The type is based not just on the data structure the class defines (instance variables), but also on the behavior included in the definition (methods).
类其实可以看成象int,long一样,也是一种数据类型,只是他不仅仅包含数据,他还有自己的行为,method.
因此,类名可以出现在任何C允许变量出现的地方.
A class name can appear in source code wherever a type specifier is permitted in C—for example, as anargument to the sizeof operator:
int i = sizeof(Rectangle);
You can use a class name in place of id to designate an object’s type:
Rectangle *myRectangle;
通过替换id类型,直接指定一个对象是什么类型,这是static typing,
Because this way of declaring an object type gives the compiler information about the kind of object it is, it’sknown as static typing. Just as id is actually a pointer, objects are statically typed as pointers to a class. Objects are always typed by a pointer. Static typing makes the pointer explicit; id hides it
对象常通过指针来定义,Static typing makes the pointer explicit; id hides it
Static typing permits the compiler to do some type checking—for example, to warn if an object could receive a message that it appears not to be able to respond to—and to loosen some restrictions that apply to objects generically typed id. In addition, it can make your intentions clearer to others who read your source code.
However, it doesn’t defeat dynamic binding or alter the dynamic determination of a receiver’s class at runtime.
Static typing 可以让编译器做更多的检测,但不影响运行时动态绑定.
Static typing 还可以用他父类来定义
Graphic *myRectangle;
At runtime, however, if the myRectangle object is allocated and initialized as an instance of Rectangle, it is treated as one.
运行时,如果确实是分配的Rectangle,还是当成Rectangle.
Type Introspection
Instances can reveal their types at runtime. The isMemberOfClass: method, defined in the NSObject class,checks whether the receiver is an instance of a particular class:
if ( [anObject isMemberOfClass:someClass] ) //check 是否是someClass
...
The isKindOfClass: method, also defined in the NSObject class, checks more generally whether the receiver inherits from or is a member of a particular class (whether it has the class in its inheritance path):
if ( [anObject isKindOfClass:someClass] ) //check 是不是继承自someClass,或者是不是someClass
...
The set of classes for which isKindOfClass: returns YES is the same set to which the receiver can be statically typed.
Introspection isn’t limited to type information. Later sections of this chapter discuss methods that return the class object, report whether an object can respond to a message, and reveal other information.
Introspection 不但能知道类型信息,还能知道一个对象能不能响应某一消息.还有其他信息.
Class Objects
A class definition contains various kinds of information, much of it about instances of the class:
● The name of the class and its superclass
● A template describing a set of instance variables
● The declarations of method names and their return and parameter types
● The method implementations
(类的信息在编译时候存储到一个数据结构中,运行时系统就是通过访问这个数据结构,创建一个类对象 (类工厂对象),这对象包含了类的所有信息,并能创建这个类的对象),
This information is compiled and recorded in data structures made available to the runtime system. The compiler creates just one object, a class object, to represent the class. The class object has access to all the information about the class, which means mainly information about what instances of the class are like. It’s able to produce
new instances according to the plan put forward in the class definition.
Although a class object keeps the prototype of a class instance, it’s not an instance itself. It has no instance variables of its own and it can’t perform methods intended for instances of the class. However, a class definition can include methods intended specifically for the class object—class methods as opposed to instance methods.
A class object inherits class methods from the classes above it in the hierarchy, just as instances inherit instance methods.
这个类对象是类实例的原型,但并不是类的实例,他没有实例变量,也不能调用实例方法. 但是,我们可以可以定义 [类方法] 从属与这个类,从而让这个类对象能调用. [类方法] 区别与
实例方法,这点跟java一样. 其继承特性跟实例方法是一样的.
In source code, the class object is represented by the class name. In the following example, the Rectangle class returns the class version number using a method inherited from the NSObject class:
int versionNumber = [Rectangle version]; //调用类对象的类方法, 类对象通过类名称代表,作为消息的接收者.
However, the class name stands for the class object only as the receiver in a message expression. Elsewhere,you need to ask an instance or the class to return the class id. Both respond to a class message:
id aClass = [anObject class];
id rectClass = [Rectangle class];
As these examples show, class objects can, like all other objects, be typed id. But class objects can also be more specifically typed to the Class data type:
Class aClass = [anObject class];
Class rectClass = [Rectangle class];
类对象既能定义成id类型,也能定义成Class 类型
typedef struct objc_class *Class; //Class 也是指针
All class objects are of type Class. Using this type name for a class is equivalent to using the class name to statically type an instance.
所有的类对象是一种Class 的数据类型,使用Class 等价于使用类的名称,使用类名则是静态类型,就象id 跟起具体的对象一样.
Class objects are thus full-fledged objects that can be dynamically typed, receive messages, and inherit methods from other classes. They’re special only in that they’re created by the compiler, lack data structures (instance variables) of their own other than those built from the class definition, and are the agents for producing instances at runtime
类对象也能dynamically typed, receive messages, and inherit methods from other classes.,不同的是他是有编译起创建,没数据结构[实例变量],是创建这类的对象的一个代理.
Note The compiler also builds a metaclass object for each class. It describes the class object just as the class object describes instances of the class. But while you can send messages to instances and to the class object, the metaclass object is used only internally by the runtime system.
编译器也生成元数据对象metaclass object ,但只是runtime system 内部自己使用.
- 对象,类,消息 4
- 对象,类,消息 1
- 对象,类,消息 2
- 对象,类,消息 3
- 对象,类,消息 5
- 对象、类和消息--ObjC
- 对象,类,消息 6 [完]
- 对象,类、方法和消息
- Objective-C 编程语言(2) 类,对象,消息 --- 对象与消息
- Objective-C 编程语言(2) 类,对象,消息 --- 类(4)类名称的用法
- 对象的消息模型
- 对象的消息模型
- 对象的消息模型
- 对象的消息模型
- 对象的消息模型
- 通过父类对象指针发送子类自定义消息
- Unity事件管理类,用于不同对象之间传递消息
- objective-c编程语言 第一章 对象,类和消息 第二小节 消息(object messaging)
- sqlite 自增 table
- 展讯8800加入一个task
- MAPPING TABLE
- 人人都能做PHP简易留言板
- 有关Eclipse不自动刷新JSP文件的解决办法(Websphere环境)
- 对象,类,消息 4
- WINCE IE png 图片不能显示
- Python xlrd、xlwt、xlutils修改Excel文件
- Windows登录类型知多少 日志秘密
- 关于open_cursors和session_cached_cursors的参数值
- 研究一下列车时刻表的后台表结构和常用的查询SQL
- 初学XWT
- Android当中的SeekBar与iOS中的UISlider 功能一致
- sql loader的使用方法