【OC语法快览】五、设计类接口

Designing a Class Interface

    设计类接口

The Objective-C syntax for creating a class is very simple. It typically comes in two parts.

创建类的语法是很简单的,通常包括两部分。

The class interface is usually stored in the ClassName.h file, and defines instance variables and public methods.

类接口通常定义实例变量和公共方法(public),保存在“类名.h(ClassName.h )”文件中。

The implementation is in the ClassName.m file and contains the actual code for these methods. It also often defines private methods that aren‘t available to clients of the class.

类接口的实现文件保存在“类名.m(ClassName.m)”文件中,包括公共方法的真正实现代码。也经常本文件中定义私有方法,私有方法不能被客户类调用。

Here‘s what an interface file looks like. The class is called Photo, so the file is named Photo.h:

下面是一个类接口文件实例,类名是Photo,所以文件名命名为Photo.h:

#import <Cocoa/Cocoa.h>

@interface Photo : NSObject {
    NSString* caption;
    NSString* photographer;
}
@end

First, we import Cocoa.h, to pull in all of the basic classes for a Cocoa app. The #import directive automatically guards against including a single file multiple
times.

首先,我们导入Cocoa.h文件,加入Cocoa应用需要的所有基础类。指令#import 保证只加入一次,即使多次使用。

The @interface says that this is a declaration of the class Photo. The colon specifies the superclass, which is NSObject.

@interface 表明这是一个Photo类声明。冒号(:)指定其父类为NSObject。

Inside the curly brackets, there are two instance variables: caption and photographer. Both are NSStrings, but they could be any object type, including id.

在大括号里面,定义了两个实例变量:caption 和 photographer
。他们都是NSStrings字符串,也可以使任意对象类型,包括id类型。

Finally, the @end symbol ends the class declaration.

最后, @end 标识结束类声明。

Add Methods

添加方法

Let‘s add some getters for the instance variables:

给实例变量添加访问器:

#import <Cocoa/Cocoa.h>

@interface Photo : NSObject {
    NSString* caption;
    NSString* photographer;
}

- caption;
- photographer;

@end        

Remember, Objective-C methods typically leave out the "get" prefix. A single dash before a method name means it‘s a instance method. A plus before a method name means it‘s a class method.

切记,OC方法通常省略掉"get" 前缀。方法前的中划线标识这是一个实例对象方法,加号则标识为类方法。

By default, the compiler assumes a method returns an id object, and that all input values are id. The above code is technically correct, but it‘s unusual. Let‘s add specific types for the return values:

编译器默认方法的返回值是一个id对象,所有的输入值也是id。上面的代码技术上是正确的,却不实用。下面的代码给方法具体化了返回值类型:

#import <Cocoa/Cocoa.h>

@interface Photo : NSObject {
    NSString* caption;
    NSString* photographer;
}

- (NSString*) caption;
- (NSString*) photographer;

@end   

Now let‘s add setters:

现在添加设值方法:

#import <Cocoa/Cocoa.h>

@interface Photo : NSObject {
    NSString* caption;
    NSString* photographer;
}
- (NSString*) caption;
- (NSString*) photographer;

- (void) setCaption: (NSString*)input;
- (void) setPhotographer: (NSString*)input;

@end        

Setters don‘t need to return a value, so we just specify them as void.

设值方法没有返回值,所以为void类型。

原文:learn_objective_C part
5

【OC语法快览】五、设计类接口

时间: 2025-01-04 05:29:30

【OC语法快览】五、设计类接口的相关文章

【OC语法快览】六、类实现

Class Implementation      类实现 Let's create an implementation, starting with the getters: 接下来创建一个类实现,从访问器开始: #import "Photo.h" @implementation Photo - (NSString*) caption { return caption; } - (NSString*) photographer { return photographer; } @en

【OC语法快览】二、存取方法

Accessors 存取方法 All instance variables are private in Objective-C by default, so you should use accessors to get and set values in most cases. There are two syntaxes. This is the traditional 1.x syntax: OC中所有的实例变量默认是私有的,所以多数情况下你应该使用访问器来获得和设置实例变量的值.访问器

【OC语法快览】三、创建实例对象

Creating Objects  创建对象 There are two main ways to create an object. The first is the one you saw before: 创建对象主要有两种方法.第一种如下: NSString* myString = [NSString string]; This is the more convenient automatic style. In this case, you are creating an autorel

【OC语法快览】四、基础内存管理

Basic Memory Management                                                           基础内存管理 If you're writing an application for Mac OS X, you have the option to enable garbage collection. In general, this means that you don't have to think about memory

【OC语法快览】一、方法调用

调用方法 [object method]; [object methodWithInput:input]; output = [object methodWithOutput]; output = [object methodWithInputAndOutput:input]; id myObject = [NSString string]; NSString* myString = [NSString string]; 嵌套消息 function1 ( function2() ); [NSSt

组件接口(API)设计指南[2]-类接口(class interface)

*返回文件夹阅读其它章节: http://blog.csdn.net/cuibo1123/article/details/39894477 类接口(class interface) 你能够參考MGTileMenu的接口文件. 我们之前谈论了一些接口的细节,这里,例举几个通用规则: 规则1:使用当前平台的描写叙述用语或构架 一个最常见的API错误设计是使用外来的规则,API属于一个特定的平台和相关开发人员生态系统. 你不能使用不论什么其它不同平台的描写叙述用语或构架,这会污染你当前的代码库,并破坏

python接口自动化(三十五)-封装与调用--流程类接口关联(详解)

简介 流程相关的接口,主要用 session 关联,如果写成函数(如上篇),s 参数每个函数都要带,每个函数多个参数,这时候封装成类会更方便.在这里我们还是以博客园为例,带着小伙伴们实践一下. 接口封装大致流程 1.在接口测试中,有些接口经常会被用到比如登录的接口,这时候我们可以每个接口都封装成一个方法,如:登录.保存草稿.发布随笔.删除随笔,这四个接口就可以写成四个方法 2.接口封装好了后,后面我们写用例那就直接调用封装好的接口就行了,有些参数,可以参数化,如保存草稿的 title 和 bod

(转载)OC语法总结

1.定义类:@interface 类名 : 父类@end 2.使用:(冒号)表示继承一个类Student : NSObject 3.使用()定义一个Catagory(类别) * 作用:在不改变原有类结构的基础上,扩展原有类的方法(不能扩展属性),但不建议重载原有类的方法 * 开发工具默认生成的文件为:类名+Catagory名称 * Catagory可以写在单独的文件中,也可以写在原有类的文件中,如何写根据需求来决定. 4.使用<>表示实现一个Protocol(协议),如需实现多个协议,将协议名

【IOS】IOS快速入门之OC语法

Objective-C 是 C 语言的超集 您还可以访问标准 C 库例程,例如在 stdlib.h 和 stdio.h 中声明的那些例程. Objective-C 还是一种非常动态的程序设计语言,而且这种动态是其最大优势.这种动态体现在它允许在运行应用程序时(即运行时)才去确定其行为,而不是在生成期间就已固定下来.因此,Objective-C 的动态机制让程序免受约束(编译和链接程序时施加的约束):进而在用户控制下,将大多数符号解析责任转移到运行时. 当您想要在源代码中包括头文件时,请在头文件或