OC的@property 和 @synthesize id
学习java的JDBC,成员变量的setter和getter,eclipse都能帮我们自动生成;当然xcode这款编译器也很强大,也能自动生成;
1:@property
@property是写在类的声明中的,具体写法;
@interface Person : NSObject
{
_age;
}
@property int age; // 这句话相当于下边的getter和setter,但是注意age没有'_',但是就是为有下划线的age生成的,这样方便了点语法的调用;
// - (void)setAge:(int)age;
// - (int)age;
@end
所以写法:@property (成员变量的类型) 去掉下划线的成员变量名
2:@synthesize:setter和getter方法的实现,在implementation中;
@implementation @synthesize age = _age; // 注意格式,等号左边表示实现age的setter和getter,即 setAge 和 age; 等号右边表示访问那个成员变量; // 代替了下面这两个方法;
/*
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
*/
@end
3:@property @synthesize后边可以同时并列多个成员变量,前提是这几个成员变量的类型一样
// @property写法
@property int age, weight; // 以逗号分隔,前提是age和weight都是int型的;
// @synthesize写法
@synthesize age = _age, weight = _weight;
4:如果自己没有明确的定义成员变量,比如_age,然后写了@property int age;这句话会自动为我们生成_age这个成员变了,但是它的访问权限是private的
5:现在版本的@property功能更强大,独揽了setter和getter的声明和实现;可以只写@property,而不用写@synthesize,xcode也会自动生成setter和getter的声明与实现,并且你也可以不用定义成员变量,它也能自动为我们生成带有下划线的成员变量,只不过是private的;
@interface Student : NSObject
@property int age; // 这句话做了3件事
/*
1:为我们生成了_age这个成员变量,private的;
2:声明了age得setter和getter方法;
3:在implementation中实现了setter和getter
*/
@end
@property @synthesize的使用注意;
如果@synthesize age; 这样写 他会访问的时和age同名的成员变量,不会访问_age; 如果没有age,他会自动生成@private的age;
方法也是一样,如果setter和getter我们自己写了,它会优先用我们自己写得,如果没有,它才会自动生成;xcode的特性就是这样,优先选择我们自己写得,如果我 们没写,它才自动生成;
id
万能指针,能指向任何OC对象,定义方式:id d = [Person new];注意定义时不要加*;如果说OC中所有的类都继承了NSObject,那么id相当于: NSObject *;
OC的@property 和 @synthesize id的更多相关文章
- OC语言@property @synthesize和id
OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...
- 李洪强iOS开发之OC语言@property @synthesize和id
OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...
- 「OC」@property @synthesize和id
一.@property @synthesize关键字 这两个关键字是编译器特性,让Xcode可以自动生成getter和setter. (一)@property 关键字 @property 关键字可以自 ...
- Objective-C 点语法 成员变量的作用域 @property和@synthesize关键字 id类型
点语法 1.利用点语法替换set方法和get方法 方法调用 Student *stu = [Student new]; [stu setAge : 18]; int age = [stu age]; ...
- OC中两个关键字的作用:@property和@synthesize
两个关键字的使用:@property和@synthesize 一.@property关键字这个关键字是OC中能够快速的定义一个属性的方式,而且他可以设置一些值,就可以达到一定的效果,比如引用计数的问题 ...
- OC语法5——@property和@synthesize
@property和@synthesize: 我们回想一下: 在OC中我们定义一个Student类需要两个文件Student.h 和 Student.m. Student.h(声明文件):定义成员变量 ...
- OC学习篇之---@property和@synthesize的使用
在之前一片文章我们介绍了OC中的内存管理:http://blog.csdn.net/jiangwei0910410003/article/details/41924683,今天我们来介绍两个关键字的使 ...
- OC开发系列-@property和@synthesize
property和synthesize 创建一个Person类.提供成员属性的_age和_height的setter和getter方法. #import <Foundation/Foundati ...
- OC基础--Property
编译器指令: 用来告诉编译器要做什么 @property: @property是编译器的指令 告诉编译器在@interface中自动生成setter和getter的声明 @synthesize: @s ...
随机推荐
- part 2 Angular modules and controllers
What is a module in AngularJS? A module is a container for different parts of your application i.e c ...
- CSS之简单树形菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- git 基本使用
简单几步操作让你在终端下用git实现文件的上传. 一.克隆项目 在工作中,常见的情景都是远程库已经建好了,需要大家把代码拉下来,共同协作开发.本文所有操作均在终端下进行. //克隆一个本地 ...
- Revit二次开发-BIM模型导出
最近一个月一直在研究Revit二次开发-BIM模型的导出,在网上找了很多相关资料学习.下面简单介绍一下我最近做的这个BIM模型的导出功能. 开始尝试使用Revit2015的样例程序里提供的读取模型几何 ...
- hdu 3665 Seaside floyd+超级汇点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3665 题意分析:以0为起点,求到Sea的最短路径. 所以可以N为超级汇点,使用floyd求0到N的最短 ...
- android ListView_新闻案例
xml设计 <?xml version="1.0"?> -<RelativeLayout tools:context=".MainActivity&qu ...
- jQuery 简单漂亮的 Nav 导航菜单
自己写的一个简单的导航菜单,先看效果: 鼠标悬浮时菜单项向上移动成蓝底白字,点击之后底部会有蓝条表示当前选中项. 页面代码,菜单的每一项都是一个 div ,其中包括一个 ul 用来放置显示文字等,另一 ...
- 存储过程及Comm.cs类的创建
2013-09-25 13:08:59 一.准备工作 首先创建一个数据库,如创建“试用期公务员管理”数据库:再创建一个Comm.cs类,添加代码如下: using System;using Syste ...
- transport
#include<iostream> using namespace std; int transport(int a) { ; ) ; else a=a/; d=; ) { a=a*+; ...
- 实验九--裸机LCD
一.环境 系统:ubuntu12.04 开发板:jz2440 编译器:gcc 二.说明 有空补上 三.代码 Makefile: CC = arm-linux-gcc LD = arm-linux-ld ...