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的更多相关文章

  1. OC语言@property @synthesize和id

    OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...

  2. 李洪强iOS开发之OC语言@property @synthesize和id

    OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...

  3. 「OC」@property @synthesize和id

    一.@property @synthesize关键字 这两个关键字是编译器特性,让Xcode可以自动生成getter和setter. (一)@property 关键字 @property 关键字可以自 ...

  4. OC @property @synthesize和id

    文顶顶   OC语言@property @synthesize和id OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字 ...

  5. OC的@property 和 @synthesize id

    学习java的JDBC,成员变量的setter和getter,eclipse都能帮我们自动生成:当然xcode这款编译器也很强大,也能自动生成: 1:@property @property是写在类的声 ...

  6. OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法

    一. 分类-Category 1. 基本用途:Category  分类是OC特有的语言,依赖于类. ➢ 如何在不改变原来类模型的前提下,给类扩充一些方法?有2种方式 ● 继承 ● 分类(Categor ...

  7. 五.OC基础--1.多态,2.类对象,3.点语法,4.@property&@synthesize,5.动态类型,内省(判断对象是否遵循特定的协议,以及是否可以响应特定的消息)

    五.OC基础--1.多态, 1. 多态概念,定义:多态就是某一类事物的多种形态: 表现形式: Animal *ani = [Dog new]; 多态条件:1.有继承关系 2.有方法的重写 2.多态代码 ...

  8. OC中@property属性关键字的使用(assign/weak/strong/copy)

    OC中@property属性关键字的使用(assign/weak/strong/copy) 一.assign 用于 ‘基本数据类型’.‘枚举’.‘结构体’ 等非OC对象类型 eg:int.bool等 ...

  9. OC基础--Property

    编译器指令: 用来告诉编译器要做什么 @property: @property是编译器的指令 告诉编译器在@interface中自动生成setter和getter的声明 @synthesize: @s ...

  10. [OC笔记]@property之个人理解,大神轻拍

    /** * 一个简单的对象 * * @author suzhen * */ public class SimpleObjcet { /** * 声明一个age字段 */ private Object ...

随机推荐

  1. VCS之Git

    Git -- open source distributed version control system -- A stream of snapshots(if no change,just lin ...

  2. NServiceBus-容器

    NServiceBus自动注册以及用户实现其所有组件处理程序和传奇,这样所有实例化模式和连接在默认情况下都是正确的,没有错误. NServiceBus在容器构建(目前Autofac的ilmerge版本 ...

  3. 无线网WEP的安全测试及防范

    650) this.width=650;" border="0" alt="" src="http://img1.51cto.com/att ...

  4. iconfont阿里妈妈前端小图标使用方法详解

    图标选购网址:http://www.iconfont.cn/ 1.从阿里妈妈网站选购好小图标,加入购物车,下载好文件: 2.把字体文件放入字体(font)文件夹(tff)(woff),(eot) 3. ...

  5. C++11用于计算函数对象返回类型的统一方法

    [C++11用于计算函数对象返回类型的统一方法] 模板 std::result_of 被TR1 引进且被 C++11 所采纳,可允许我们决定和使用一个仿函数其回返值的类别.底下,CalculusVer ...

  6. FATFS文件系统

    STM32移植文件系统,操作SD卡,对SD卡进行读写 FATFS文件系统与底层介质的驱动分离开来,对底层介质的操作都要交给用户去实现,它仅仅是提供了一个函数接口而已,函数为空,要用户添加代码.然后 F ...

  7. GRUB加密

    在 /etc/grub.conf 内添加password=密码(也可使用加密的密码password= --md5 加密过的密码) 如何获得加密密码? 那就是grub-md5-crypt命令 简单流程如 ...

  8. CSS圆角,输入框提示信息,JS查找同级元素

    input { /*设置边框*/ border:1px solid #95B8E7; border-radius: 5px; /*设置圆角,IE不兼容*/ height:18px } placehol ...

  9. thinkphp 防止sql注入

    防止SQL注入 对于WEB应用来说,SQL注入攻击无疑是首要防范的安全问题,系统底层对于数据安全方面本身进行了很多的处理和相应的防范机制,例如: $User = M("User") ...

  10. VC中常用的宏[转]

    我们在VS环境中开发的时候,会遇到很多宏定义,这些宏可以应用到代码中,或用于编译.工程选项等设置,总之是我们开发中必不可少的工具,有必要做一个总结.有些宏是C/C++定义的,有些宏是VC环境预定义的. ...