定义类1

来源:互联网 发布:小店收快递软件 编辑:程序博客网 时间:2024/04/30 22:58
Much of object-oriented programming consists of writing the code for new objects—defining new classes. In Objective-C, classes are defined in two parts:
● An interface that declares the methods and properties of the class and names its superclass

● An implementation that actually defines the class (contains the code that implements its methods)


Each of these parts is typically in its own file. Sometimes, however, a class definition spans several files through the use of a feature called a category . Categories can compartmentalize a class definition or extend an existing one. Categories are described in “Categories and Extensions” (page 73).


类的定义包括接口的声明与实现两部分,有时候可以利用category  特性把类的定义拆开成几个文件.


A single file can declare or implement more than one class. Nevertheless, it’s customary to have a separate interface file for each class, if not also a separate implementation file. Keeping class interfaces separate better  reflects their status as independent entities.

一个单独的文件可以声明与实现多个类,不管如何,即使不将类的实现分开在不同的文件,通常也将每一个类的声明放在单独的文件中,这钟做法可以很好的保持每一个实体的独立性


Interface and implementation files typically are named after the class. The name of the implementation file has the .m extension, indicating that it contains Objective-C source code. The interface file can be assigned  any other extension. Because it’s included in other source files, the name of the interface file usually has the
.h extension typical of header files. For example, the Rectangle class would be declared in Rectangle.h  and defined in Rectangle.m.

实现文件命名通常是类后加.m 结尾,暗示包含Object C 代码,而接口文件则可以任意的后缀,除.m, 通常是.h . Rectangle  类:Rectangle.h  and  Rectangle.m.


Class Interface

接口的声明一编译指令@interface 开始,@end结束,所有的Object c 编译指令都是以@开始的.
The declaration of a class interface begins with the compiler directive @interface and ends with the directive @end. (All Objective-C directives to the compiler begin with “@”.)
@interface ClassName : ItsSuperclass
// Method and property declarations.
@end


The first line of the declaration presents the new class name and links it to its superclass. The superclass defines  the position of the new class in the inheritance hierarchy, as discussed under “Inheritance” (page 19).  Methods and properties for the class are declared next, before the end of the class declaration. The names of
methods that can be used by class objects, class methods, are preceded by a plus sign:
+ alloc; //类对象方法以+ 作为前缀标志
The methods that instances of a class can use, instance methods, are marked with a minus sign:
- (void)display;  //实例方法则以-


虽然不是一个通常的做法,我们可以定义相同的类方法名与实例方法名, 但我们往往是定义相同的方法名称跟实例变量名,以用来暗示该方法返回这个实例变量.

Although it’s not a common practice, you can define a class method and an instance method with the same  name. A method can also have the same name as an instance variable, which is more common, especially if  the method returns the value in the variable. For example, Circle has a radius method that could match a
radius instance variable.


Method return types are declared using the standard C syntax for casting one type to another:
- (float)radius;
Parameter types are declared in the same way:

- (void)setRadius:(float)aRadius;   //注意方法名是包括:冒号的.

If a return or parameter type isn’t explicitly declared, it’s assumed to be the default type for methods and  messages—an id. The alloc method illustrated earlier returns id.

如果方法没有指明返回类型,则默任是id


When there’s more than one parameter, the parameters are declared within the method name after the colons. Parameters break the name apart in the declaration, just as in a message. For example:

//多余一个参数时候,参数的声明是以冒号分隔而包含在方法名称中的.

- (void)setWidth:(float)width height:(float)height;   //方法名称是:setWidth:height:   ///:也是算到方法名称中的.:height 也是方法名的一部分.
Methods that take a variable number of parameters declare them using a comma and ellipsis points, just as a
function would:
- makeGroup:group, ...;


Property declarations take the form:  //属性的声明
@property (attributes) Type propertyName;
Properties are discussed in more detail in “Declared Properties” (page 62).


Note Historically, the interface required declarations of a class’s instance variables, the data structures that are part of each instance of the class. These were declared in braces after the @interface  declaration and before method declarations:


@interface ClassName : ItsSuperclass  //历史版本的Object C 类声明中实例变量是放到
{
// Instance variable declarations.    //这里的,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,{括号里}, 方法的声明前....
}
// Method and property declarations.
@end


Instance variables represent an implementation detail, and should typically not be accessed outside  of the class itself. Moreover, you can declare them in the implementation block or synthesize (合成) them   using declared properties. Typically you should not, therefore, declare instance variables in the public
interface and so you should omit the braces.


@interface ClassName : ItsSuperclass   //Object 2.0 后,就可以忽略{} 了...通过declared  properties
// Method and property declarations.
@end


Importing the Interface (P35)

The interface file must be included in any source module that depends on the class interface—that includesany module that creates an instance of the class, sends a message to invoke a method declared for the class,or mentions an instance variable declared in the class. The interface is usually included with the#importdirective:

#import "Rectangle.h"

This directive is identical to#include,except that it makes sure that the same file is never included morethan once. It’s therefore preferred and is used in place of#includein code examples throughoutObjective-C–based documentation.

To reflect the fact that a class definition builds on the definitions of inherited classes, an interface file begins  by importing the interface for its superclass:

使用指令#import 可以在引入接口的声明,作用跟#include 是一样的,只是#import 可以避免重复引用。

#import "ItsSuperclass.h" // 通常一个类的声明都是从引用他的父类的接口文件开始的。
@interface ClassName : ItsSuperclass// Method and property declarations.@end


This convention means that every interface file includes, indirectly, the interface files for all inherited classes.When a source module imports a class interface, it gets interfaces for the entire inheritance hierarchy that theclass is built upon.

Note that if there is aprecomp—aprecompiled header—that supports the superclass, you may prefer toimport the precomp instead.

Referring to Other Classes

一个类的接口声明通过引入其父类隐式的包含所有的继承类,从根类NSObject 到该类的直接父类,如果

在类的声明中引用到了其他不在这个继承链的类,则需要显式的引入其接口文件,或使用@class 指令

做前向声明,这前向声明不会引入他们的接口文件。

An interface file declares a class and, by importing its superclass, implicitly contains declarations for all inherited  classes, fromNSObjecton down through its superclass. If the interface mentions classes not in this hierarchy,it must import them explicitly or declare them with the@classdirective:

This directive simply informs the compiler that “Rectangle” and “Circle” are class names. It doesn’t import their interface files.

An interface file mentions class names when it statically types instance variables, return values, and parameters.For example, this declaration

@class Rectangle, Circle;  //告诉编译器这两条是类。
- (void)setPrimaryColor:(NSColor *)aColor;

mentions theNSColorclass.

因为这种声明只是简单的使用这个类名作为一种类型,而不依赖任何类的接口声明(其方法,实例变量),@class 告诉编译器足够的前向信息

,这个是一个类,就这样。然而,当这个类真正使用时如实例创建,发送消息,作为前向声明的类接口时必须引入的,通常接口文件使用@class

声明类,而在对应时实现文件里面引入前向类的接口文件。

Because declarations like this simply use the class name as a type and don’t depend on any details of the class  interface (its methods and instance variables), the@classdirective gives the compiler sufficient forewarning  of what to expect. However, when the interface to a class is actually used (instances created, messages sent),the class interface must be imported. Typically, an interface file uses @classto declare classes, and the corresponding implementation file imports their interfaces (since it needs to create instances of those classesor send them messages).


The@classdirective minimizes the amount of code seen by the compiler and linker, and is therefore the  simplest way to give a forward declaration of a class name. Being simple, it avoids potential problems that  may come with importing files that import still other files. For example, if one class declares a statically typed  instance variable of another class, and their two interface files import each other, neither class may compile  correctly.



The Role of the Interface

The purpose of the interface file is to declare the new class to other source modules (and to other programmers).It contains information they need to work with the class (programmers might also appreciate a littledocumentation).

The interface file tells users how the class is connected into the inheritance hierarchy and what otherclasses—inherited or simply referred to somewhere in the class—are needed. 接口文件告诉用户这个类如何接入到继承链,和其他需要引用到的类

Through its list of method declarations, the interface file lets other modules know what messages can besent to the class object and instances of the class.Every method that can be used outside the class definitionis declared in the interface file; methods that are internal to the class implementation can be omitted. 虽然只是列出方法的声明,但接口文件可以让其他模块知道哪些消息可以发送,拥有哪些实例变量,还有一段不知道要告诉方法的作用域,需要实践,,,,,?????