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. 二叉树单色路径最长&&穿珠子

    对树的操作,特别理解递归的好处. //对于一棵由黑白点组成的二叉树,我们需要找到其中最长的单色简单路径,其中简单路径的定义是从树上的某点开始沿树边走不重复的点到树上的 //另一点结束而形成的路径,而路 ...

  2. Android之操作SQLite

    一.SQLite的介绍 1.SQLite简介 SQLite是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入  式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的 ...

  3. 通过ajax提交form表单

    $.ajax({ url : 'deliveryWarrant/update.do', data : $('#myform').serialize(), type : "POST" ...

  4. Ubuntu 下无法Tab键自动补全功能解决办法

    在Ubuntu下 使用Tab键报错:cannot create temp file for here-document: no space left on device 解决: rm -rf /var ...

  5. 移动端和web端前端UI库—Frozen UI、WeUI、SUI Mobile

    web http://www.pintuer.com/ 拼图 http://www.h-ui.net/ http://www.layui.com/  很厉害的一个个人产品 http://amazeui ...

  6. ntpdate server时出错原因及解决

    错误1.Server dropped: Strata too high 在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronizati ...

  7. 1.VS2010C++环境设置

    一.需要下载的软件 1.visual studio 2010\\xxzx\tools\编程工具\MICROSOFT\VISUAL.STUDIO\VISUAL.STUDIO.201032位cn_visu ...

  8. 2.VS2010C++各种后缀说明

    相关资料:"http://blog.csdn.net/kibaamor/article/details/18700607""http://blog.chinaunix.n ...

  9. Labview中的属性节点

    获取(读取)和/或设置(写入)引用的属性.通过属性节点对本地或远程应用程序实例.VI或对象获取或设置属性和方法也可通过属性节点访问LabVIEW类的私有数据. 属性节点可自动调整为用户所引用的对象的类 ...

  10. HDU 4667 Building Fence

    题意: 给n个圆和m个三角形,且保证互不相交,用一个篱笆把他们围起来,求最短的周长是多少. 做法:--水过... 把一个圆均匀的切割成500个点,然后求凸包. 注意:求完凸包,在求周长的时候记得要把圆 ...