oc-25- @property @synthesize
s.h #import <Foundation/Foundation.h> @interface Student : NSObject
{
@public
NSString *_name;
int _age;
int _height;
} // @property能够自动生成set和get方法的 声明
// @property 成员变量类型 成员变量名称(去掉下划线);
//- (void)setName:(NSString *)name;
//- (NSString *)name;
@property NSString *name;
@end s.m /**
问题:想要给自己不带下划线的成员变量进行赋值,怎么办?> 需要给@synthesize指定,告诉该赋值给谁.
@synthesize name = _name;
它就知道,赋值_name;
*/ #import "Student.h" @implementation Student
@synthesize name;
//生成了getset方法的实现
//- (void)setName:(NSString *)name
//{
// name = name;
// NSLog(@"%p",name);
//}
//- (NSString *)name
//{
//
// return name;
//}
@end main.m #import <Foundation/Foundation.h>
#import "Student.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *s = [Student new];
s.name = @"亚索";
// 这步能够调用,证明@property生成了set和get方法的声明.
[s setName:@"亚索"];
// 证明@synthesize生成了set和get方法的实现.
NSLog(@"%p",s->name);
NSLog(@"-----");
}
return ;
}
@property增强使用
- Xcode4.4版本以后支持的
- 只使用 @property 进行声明,类自动帮你实现。
Xcode4.4以后property做了增强
- 帮助我们自动生成get/set方法的声明
- 帮助我们自动生成get/set方法的实现
s.h
/**
@property的加强用法:
1.生成set和get方法的声明
2.生成set和get方法的实现
3.生成带下划线的成员变量. 注意事项:
1. 当用户手动重写了set方法时,@property会生成get方法和带下划线的成员变量
2. 当用户手动重写了set和get方法时.@property不会生成待下划线的成员变量.
3. 当用户手动重写了get方法时,@property会生成set方法和带下划线的成员变量. */ #import <Foundation/Foundation.h>
#import "Person.h" @interface Student : Person @property NSString *name;//生成的变量名是_name, @property int age; @property int height; @property int weight; @end s.m #import "Student.h" @implementation Student //@synthesize age = _age,height = _height,weight = _weight,name = _name; //手动重写get方法
- (NSString *)name
{
return _name;
} - (instancetype)init
{
if (self = [super init]) {
NSLog(@"s---%@",self);
NSLog(@"s---%@",super.class);
}
return self;
}
@end
oc-25- @property @synthesize的更多相关文章
- 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 关键字可以自 ...
- OC @property @synthesize和id
文顶顶 OC语言@property @synthesize和id OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字 ...
- OC的@property 和 @synthesize id
学习java的JDBC,成员变量的setter和getter,eclipse都能帮我们自动生成:当然xcode这款编译器也很强大,也能自动生成: 1:@property @property是写在类的声 ...
- OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法
一. 分类-Category 1. 基本用途:Category 分类是OC特有的语言,依赖于类. ➢ 如何在不改变原来类模型的前提下,给类扩充一些方法?有2种方式 ● 继承 ● 分类(Categor ...
- 五.OC基础--1.多态,2.类对象,3.点语法,4.@property&@synthesize,5.动态类型,内省(判断对象是否遵循特定的协议,以及是否可以响应特定的消息)
五.OC基础--1.多态, 1. 多态概念,定义:多态就是某一类事物的多种形态: 表现形式: Animal *ani = [Dog new]; 多态条件:1.有继承关系 2.有方法的重写 2.多态代码 ...
- OC中@property属性关键字的使用(assign/weak/strong/copy)
OC中@property属性关键字的使用(assign/weak/strong/copy) 一.assign 用于 ‘基本数据类型’.‘枚举’.‘结构体’ 等非OC对象类型 eg:int.bool等 ...
- OC基础--Property
编译器指令: 用来告诉编译器要做什么 @property: @property是编译器的指令 告诉编译器在@interface中自动生成setter和getter的声明 @synthesize: @s ...
- [OC笔记]@property之个人理解,大神轻拍
/** * 一个简单的对象 * * @author suzhen * */ public class SimpleObjcet { /** * 声明一个age字段 */ private Object ...
随机推荐
- Python【基础第二篇】
元组 元组的元素不可修改 元组的元素的元素可修改 字典 Python主文件判断 name == main python中一切事物都是对象 对象是基于类创建的 对象具有的所有功能都是从类里找的 int内 ...
- group_concat 长度限制,排序和设置分隔符
select aid,group_concat(bid order by bid separator ',') as bid_str from tbl group by aid; SET GLOBAL ...
- 【转】类中如何引用server.MapPath()
转至:http://blog.csdn.net/tangjianft/article/details/5357151 今天在写一个上传图片方法时遇到了两个问题:1.public string getI ...
- initWithSpriteFrameName和createWithSpriteFrameName
/** * Initializes a sprite with a sprite frame name. <br/> * A cc.SpriteFrame will be fetched ...
- mysql-python模块编译问题解决
解决方法:yum -y install mysql-devel libxml2 libxml2-dev libxslt* zlib gcc openssl [root@localhost MySQL- ...
- POJ 2318 TOYS (计算几何,叉积判断)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8661 Accepted: 4114 Description ...
- 一键安装GitLab7
1. Install and configure the necessary dependencies If you install Postfix to send email please sele ...
- datagridview里面有combox避免双击两次的写法
双击两次变成单击一次的写法: void dataGridView_CellEnter(object sender, DataGridViewCellEventArgs e) { //实现单击一次显示下 ...
- StructLayout特性
StructLayout特性 公共语言运行库利用 StructLayoutAttribute控制类或结构的数据字段在托管内存中的物理布局,即类或结构需要按某种方式排列.如果要将类传递 ...
- CStdioFile
CStdioFile类的声明保存再afx.h头文件中. CStdioFile类继承自CFile类,CStdioFile对象表示一个用运行时的函数fopen打开的c运行时的流式文件.流式文件是被缓冲的, ...