[Objective-C] class add a property and its setter and getter
2015-06-29 18:28
609 查看
参考:
http://cocoadevcentral.com/d/learn_objectivec/
/article/1227605.html
http://www.88cto.com/996655/article/details/21674.html
/article/8320208.html
有两种方法,方法一较简洁方便,方法二较灵活。
方法一:
使用 @property 和 @synthesize 关键字。setter 和 getter 方法会编译器被自动生成。详情参考 [Objective-C]property setter/getter via @property
方法二:
自己定义property、setter方法、getter方法。
// MyTest.h
注:
Object C的类的getter函数省略了getXXX的"get",比如类的某属性叫caption,getter函数也叫caption。setter函数叫setCaption
http://cocoadevcentral.com/d/learn_objectivec/
/article/1227605.html
http://www.88cto.com/996655/article/details/21674.html
/article/8320208.html
有两种方法,方法一较简洁方便,方法二较灵活。
方法一:
使用 @property 和 @synthesize 关键字。setter 和 getter 方法会编译器被自动生成。详情参考 [Objective-C]property setter/getter via @property
// ========= Person.h ========= @interface Person: NSObject { } -(id) Print; @property NSString* name; @end // ========= Person.m ========= #import "Person.h" @implementation Person -(id) Print { NSLog(@"Print_Name:%@", _name); // #100 } @end // ========= main.mm ========= #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { Person* p = [[Person alloc] init]; [p setName:@"Henry"]; NSLog(@"Name:%@", [p name]); [p Print]; } }
方法二:
自己定义property、setter方法、getter方法。
// MyTest.h
#import <Foundation/Foundation.h> @interface MyTest : NSObject { NSString* caption; // caption MUST be declared here } - (NSString*) caption; - (void) setCaption: (NSString*) input; @end// MyTest.h
#import "MyTest.h" @implementation MyTest { } - (NSString*) caption { return caption; } - (void) setCaption: (NSString*) input { caption = input; } @end
注:
Object C的类的getter函数省略了getXXX的"get",比如类的某属性叫caption,getter函数也叫caption。setter函数叫setCaption
相关文章推荐
- objective-c中的category
- Java - Why wait, notify and notifyAll is defined in Object Class and not on Thread class
- json学习系列(6)JSONObject和JSONArray是JDK的集合部分延伸
- NSDictionary、NSMutableDictionary的基本用法
- JSONObjectWithData方法里options参数选择解释
- json学习系列(3)-JSONObject的过滤设置
- json学习系列(2)-生成JSONObject的方法
- 关于C++和Objective-C混编
- Effective Objective-C 2.0之Note.04
- Object-C基础--输入5个字符串并排序
- Objective-C语法学习 第四天
- 【iOS开发之Objective-C】Objective-C语言中的OOP
- object-c 全屏播放时动态添加slider
- 关于objective—c如何让JPG文件转化成PDF形式
- Qt源码分析之QObject(转自CSDN,作者oowgsoo)
- [Objective-C] 理解 Objective-C 的 ARC
- java平台下arcobjects的开发
- [Objective-C] iOS 开发中建议使用自动引用计数(ARC)
- [Objective-C] 从“引用计数”过渡到“自动引用计数”(ARC)
- 使用Xcode HeaderDoc和Doxygen文档化你的Objective-C和Swift代码