object-c 学习第四天(类方法和接口)

类方法和接口

Class Methods

虽然之前有给类实例送消息的例子了,你也可以给类本身传递消息,(一个类是一个在运行时被创建的),类方法就像C++中的静态方法

 

Although the precedingexamples sent messages to an instance of a class, you can also send messages tothe class itself. (A class is an object of type Class created by the runtime.) Whenmessaging a class, the method you specify must be defined as a class methodinstead of an instance method. Class methods are a feature similar to staticclass methods in C++.

 

在如下这个例子中如何运用工厂模式,这个array方法是一个NSArray的类方法,NSMutableArray继承它,在这个类里面分配并且初始化类的新实例。

The following exampleillustrates how you use a class method as a factory methodfor a class. In this case, the array method is a class method on the NSArray class—and inherited by NSMutableArray—that allocates and initializes a new instance of the class and returns itto your code.

NSMutableArray *myArray = nil;  // nil is essentially the same as NULL

 

// Create a new array and assign it to the myArray variable.

myArray = [NSMutableArray array];

 

 

Declared Properties and Accessor Methods

一个property其实就是一个接口,下面直接叫接口了)一般意义上一些被封装的数据或者被一个存放一个对象,它也可以是一个属性(一个名字或者一个颜色或者是与一个或者多个对象的关系),这样对象定义了一个接口可以让对象的使用者设定和使用被封装的属性,做这些动作的方法我们成为“accessor methods

A property in thegeneral sense is some data encapsulated or stored by an object. It is either anattribute—such as a name or a color—or a relationship to one or more otherobjects. The class of an object defines an interface that enables users of itsobjects to get and set the values of encapsulated properties. The methods thatperform these actions are called accessor methods.

 

有两种accessor methods ,每个方法都必须遵守一个种命名的约定,一个“getter”方法,返回一个属性的值,有和这个属性一样的名字一个“setter”方法,可以给一个属性赋值从而形成PropertyName:(第一个字母要大写哦),有合适名字的“accessor method”是很多CocoaCocoa触摸框架的评判元素,包括关键值编码,                                               

 

There are two types ofaccessor methods, and each method must conform to a naming convention. A“getter” accessor method, which returns the value of a property, has the same name as the property. A"setter” accessor method, which sets a new value for a property, has the form setPropertyName:, where the first letter of theproperty name is capitalized. Properly named accessor methods are a criticalelement of several technologies of the Cocoaand Cocoa Touch frameworks, including key-value coding (KVC), which is amechanism for accessing an object’s properties indirectly through their names.

 

 

已经声明的接口可以排除为每个在类中属性实现gettersetter方法的需要。然而,你要指定一个行为:你要对你想要用的接口进行声明。然后编译器会去创建——或者合成——事实上gettersetter方法都基于那个声明。已经声明的接口可以减少你写引用代码的量,因此,为了让你的代码更干净并且减少出错的可能性。用接口或者“accessor methods”来获取或者设定对象的状态。

Declared properties eliminate the need to implement a getter and settermethod for each property exposed in the class. Instead, you specify thebehavior you want using the property declaration. The compiler can thencreate—or synthesize—actual getter andsetter methods based on that declaration. Declared properties reduce the amountof boilerplate code you have to write and, as a result, make your code muchcleaner and less error prone. Use declared properties or accessor methods toget and set items of an object’s state.

 

在你的类中你用方法声明把接口的声明包含进去。你在类的头文件里声明公共的接口;你在继承类中声明私有接口,对象控制器的接口,例如“delegates“和视图控制器就是典型的私有接口。

 

You include propertydeclarations with the method declarations in your class interface. You declarepublic properties in the class header files; you declare private properties ina class extension in the source file. (See “Protocols and Categories” for ashort description of class extensions along with an example.) Properties ofcontroller objects such as delegates and view controllers should typically beprivate.

 

 

一个接口最基本的声明就是用@property编译指令,随后就是类型信息和接口的名字。你也可以用传统的选项来配置接口,这个选项可以用来定义access methods的动作,无论接口是一个弱引用或者是一个只读。这个选项插入在@property指令后面。

例子:

@property (copy) MyModelObject *theObject;  // 在分配任务的时候复制对象

@property (readonly) NSView *rootView;      // 只声明一个getter方法

@property (weak) id delegate;               // 声明一个委托作为一个弱引用

 

 

The basic declarationfor a property uses the @property compiler directive, followed by the type information and name of theproperty. You can also configure the property with custom options, which definehow the accessor methods behave, whether the property is a weak reference, andwhether it is read-only. The options are in parentheses following the @property directive.

The following lines ofcode illustrate a few more property declarations:

@property (copy) MyModelObject *theObject;  // Copy the object during assignment.

@property (readonly) NSView *rootView;      // Declare only a getter method.

@property (weak) id delegate;               // Declare delegate as a weak reference

 

 

编译器会自动合成已经声明的接口。在合成一个接口的时候,编译器会创建一个“access method”并且一个私有的实例会“返回”?接口。这个实例变量和接口有相同的名字但是在前面会有一个额外的”_”只有在用对象初始化和重新分配内存方法时,你的应用程序应该直接和实例变量相连(而不是它原来的接口)

The compilerautomatically synthesizes declared properties. In synthesizing a property, itcreates accessor methods for it as well as a private instance variable that“backs” the property. The instance variable has the same name as the propertybut with an underscore prefix (_). Your app should directly access an instance variable (instead of itsproperty) only in methods for object initialization and deallocation.

 

如果你给一个实例变量换一个不要的名字,你可以绕过自动合成并且显示地合成一个接口。在类实现的时候用指令@synthesize来让编译器产生“access methods”和你自己特别命名的实例变量,例如:

@synthesize enabled = _isEnabled;

 

If you want adifferent name for an instance variable, you can bypass autosynthesis andexplicitly synthesize a property. Use the @synthesize compiler directive in the classimplementation to ask the compiler to generate the accessor methods along withthe specially named instance variable. For example:

 

当你声明一个你自己命名的“accessor method”的接口时,你要特别的让布尔型的getter方法遵循一个传统的格式,就像这样:

@property (assign, getter=isEnabled) BOOL enabled; // Assign new value, change name of getter method

 

Incidentally, when youdeclare a property you can specify custom names for the accessor methods,typically to make the getter methods of Boolean properties follow aconventional form, as shown here:

 

你可能感兴趣的:(properties,Class,compiler,getter,methods,accessor)