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 ...
随机推荐
- ShellExecute的各种用法
一.利用系统默认的邮件收发器发送电子邮件 Uses ..., ShellAPI; Var lpHwnd: HWND; lpOperation, lpFile, lpParameters, lpDire ...
- 40个最好的Tumblr主题
如果安装了一款较好的Tumblr主题,你的Tumblr空间将焕然一新.然而找到一款合适的主题并不是一件容易的事,这正是本文中我整理那么多优质的Tumblr模板作为灵感的原因.其中有一些免费的Tumbl ...
- ZeroCopyLiteralByteString cannot access superclass
问题描述 在HBase上运行MapReduce作业时,报如下异常:IllegalAccessError: class com.google.protobuf.HBaseZeroCopyByteStri ...
- IIS启动出错解决方法
IIS出现server application error,最终解决办法2007年10月30日 星期二 20:38Server Application Error The server has enc ...
- 得到python某个模块的路径
#-*-coding:utf-8-*- # 导入imp模块 import imp # 打印出MySQLdb模块 print imp.find_module("MySQLdb")
- Debian openvpn 配置
1.安装openvpn 和 iptables -- Debain 可以使用命令行`apt-get install openvpn iptables` 2.配置服务器 -- ```shell cp -R ...
- poj 2196 Specialized Four-Digit Numbers
如果一个数字 十进制的各位数的和 == 十六进制的各位数的和 == 十二进制的各位数的和,则输出,从2992到9999 #include <cstdio> int toDD(int n) ...
- Working with Sprites
[Working with Sprites] 1.An SKSpriteNode object can be drawn either as a rectangle with a texture ma ...
- BootCamp支持软件4/5
按 Mac 机型列出的 Boot Camp 要求 不同的 Mac 电脑适用不同版本的 Windows.如果您不知道您拥有的 Mac 是什么机型,请从 Apple 菜单中选取“关于本机”. 每个表格条目 ...
- 时隔3年,再次折腾BlackBerry 8830!
2010年手头换得8830,之后就是好几番刷机.解SPC.倒腾各种软件..算软件注册码..那个时候记得最难弄的注册码就是crunchSMS.需要运行虚拟机来从内存地址读取注册码..不过黑莓真的很经得起 ...