当前位置:网站首页>【Objective-C语言中的@property】
【Objective-C语言中的@property】
2022-07-31 06:28:00 【清风清晨】
前言
我们写一个类,分以下几个步骤:
1)要先为类写属性
2)再声明属性的getter 和 setter方法
3)再实现这个getter和setter
有没有让编译器自动实现这些代码的方法?
答案是,有,天空一声巨响,@property就隆重登场了!
一、@property的作用是什么?
1)作用:自动生成getter和setter方法的声明
因为是生成方法的声明,所以应该写在@interface类的声明里面。
2)语法:@property 数据类型 名称;
比如,有一个Person类:
@interface Person : NSObject
{
NSStrin *_name;
int _age;
}
– (void)setName:(NSStrin *)name;
– (NSString *)name;
@property int age;
@end
@implementation Person
– (void)setName:(NSString *)name
{
_name = name;
}
– (NSString *)name
{
return _name;
}
@end
上述代码中的@property int age,这句话有什么效果?
1)它自动的生成getter和setter方法的声明
2)它的原理:
编译器在编译的时候,会根据@property生成getter和setter方法的声明:
@property 数据类型 名称;
这句话会被编译器转换为:
- (void)set首字母大写的名称:(数据类型)名称;
- (数据类型)名称;
例如:
@property int age;
会被编译器自动转换为以下两个语句:
- (void)setAge:(int)age;
- (int)age;
再比如,我们想让@property自动生成以下两句代码:
- (void)setName:(NSString *)name;
- (NSString *)name;
怎么做?只需写出如下一句代码:
@property NSString *name;
二、使用@Property的注意:
1.数据类型和属性的数据类型一致
例如,@property float height;
[email protected]的名字和属性的名字一致,但是要去掉下划线
例如,@property float weight;
[email protected]的名称决定了我们这个getter和setter方法的名称
[email protected]的数据类型决定了setter方法的参数类型,和getter方法的返回值类型
总结
@property自动生成getter和setter方法的声明,但是getter和setter方法的实现,还要自己写,属性还要自己定义。
边栏推荐
- 解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
- DAY18: Xss Range Clearance Manual
- Zabbix6.2 Surprise Release!Especially optimize the performance of medium and large environment deployment!
- 文件 - 04 下载文件: 根据文件下载链接下载文件
- 2022.07.14_每日一题
- Automatic translation software - batch batch automatic translation software recommendation
- LeetCode:952. 按公因数计算最大组件大小【欧拉筛 + 并查集】
- Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
- 关于求反三角函数的三角函数值
- Detailed explanation of js prototype
猜你喜欢
Difficulty comparison between high concurrency and multithreading (easy to confuse)
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
DAY18:XSS 漏洞
超级详细的mysql数据库安装指南
【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
Zabbix6.2 Surprise Release!Especially optimize the performance of medium and large environment deployment!
【解决】npm ERR A complete log of this run can be found in npm ERR
Linked list implementation and task scheduling
剑指offer(一)
DAY18: XSS vulnerability
随机推荐
2022.07.13_每日一题
Tasks and task switching
把 VS Code 当游戏机
2022.07.15_Daily Question
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
Yu Mr Series 】 【 2022 July 022 - Go Go teaching course of container in the dictionary
简单谈谈Feign
R——避免使用 col=0
2022.07.20_Daily Question
tidyverse笔记——tidyr包
2022.07.26_Daily Question
【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
PCB抄板
Zabbix6.2 Surprise Release!Especially optimize the performance of medium and large environment deployment!
Automatic translation software - batch batch automatic translation software recommendation
事务的传播机制
2022.07.18 _ a day
在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
链表实现及任务调度
多进程全局变量失效、变量共享问题