您的位置:首页 > 移动开发 > Objective-C

IT English Collection(22)of Accessor method

2013-10-18 18:12 453 查看

1 前言

    本章主要介绍了Objective-C中的存取方法的相关内容。

2 详述

2.1 原文

  An
accessor method is an instance method that gets or sets the value of a property of an object. In Cocoa’s
terminology, a method that
retrieves the value of an object’s property is referred to as a getter method, or “getter;” a method that changes the value of an object’s property is referred to as a setter method, or “setter.” These methods are often found in pairs, providing API
for getting and setting the property values of an object.

  You should use accessor methods rather than directly accessing state data because they provide an abstraction layer. Here are just two of the benefits that accessor methods provide:

·You don’t need to rewrite your code if the manner in which a property is
represented or stored changes.

·Accessor methods often implement important behavior that occurs whenever a value is retrieved or set. For example, setter methods
frequently implement memory management code and notify other objects when a value is changed.

Naming Conventions

  Because of the importance of this pattern, Cocoa defines some conventions for naming accessor methods. Given a property of type
type and called name, you should typically implement accessor methods with the following form:

- (type)name;
- (void)setName:(type)newName;


  The one exception is a property that is a Boolean value. Here the getter method name may be isName. For example:

- (BOOL)isHidden;
- (void)setHidden:(BOOL)newHidden;


  This naming convention is important because much other
functionality in Cocoa
relies upon it, in particular key-value coding. Cocoa does not use getName because methods that start with “get” in Cocoa
indicate that the method will return values by reference.

2.2 生词

accessor n. [计]
存取器;访问器
terminology
[,tə:mi'nɔlədʒi]n. 术语,术语学;用辞
retrieve
[ri'tri:v]vt. [计] 检索;恢复
abstraction
[əb'strækʃ(ə)n]n. 抽象;提取
benefit
['benɪfɪt]n. 利益,好处

manner ['mænə]n.
方式;习惯
represented
[reprɪ'zentɪd]v. 代表;表现;描写
behavior
[bi'heivjə]n. 行为,举止
frequently
['friːkw(ə)ntlɪ]adv. 频繁地,经常地;
Convention
[kən'venʃ(ə)n] n. [法]
惯例;会议
functionality
[,fʌŋkʃə'næliti]n. 功能;

relies 信任
particular
[pə'tɪkjʊlə]adj. 特别的;详细的
indicate
['ɪndɪkeɪt]vt. 表明;指出

3 结语

    以上是所有内容,希望对大家有所帮助。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息