@property和@synthesize
|
#import <Foundation/Foundation.h> #import "Student.h" int main(int argc, constchar * argv[]) { @autoreleasepool { Student *stu = [[Studentalloc] init]; stu.age = 11; NSLog(@"age is %i",stu.age); } return 0; } |
|
#import <Foundation/Foundation.h> @interface Student : NSObject{ //默认是@protected int age; int age1; int age2; int no; float height; } //当编译器遇到@property时,会自动展开成getter和setter的声明 一行相当于两行 @property不可以省略 @propertyint age; //-(void)setAge:(int)newAge; //-(int)age; @propertyint no; //-(void)setNo:(int)newNo; //-(int)no; @propertyfloat height; //-(void)setHeight:(int)newHeight; //-(float)height; @end |
|
#import "Student.h" @implementation Student //在xcode4.5及之后的版本,可以省略@synthesize,并且默认去访问_age这个成员变量,如果找不到会自动生成一个叫_age的私有成员变量 //@synthesize age,height,no;也可以这样写 @synthesize age; //@synthesize会自动生成getter和setter的实现 //-(void)setAge:(int)newAge{ //@synthesize 是默认访问跟 “synthesize <#property#>”中与“<#property#>” 相同的成员变量,如果找不到就会 // age = newAge; //生成一个私有的同名变量 <#property#> //} //-(int)age{ // return age; //} @synthesize no; //如果已经定义了成员变量int _no,则需要这样写:@synthesize no = _no;代表getter和setter去访问_age这个成员变量;之后 //-(void)setNo:(int)newNo{ //变量no不存在了!!! // no = newNo; //} //-(int)no{ // return no; //} @synthesize height; //-(void)setHeight:(float)newHeight{ // height = newHeight; //} //-(float)height{ // return height; //} @synthesize wide; //@synthesize wide;表明已在.h文件中声明了一个成员变量 wide,即不用再声明成员变量wide了。 @end |
@property和@synthesize的更多相关文章
- Objective-C中的@property和@synthesize用法
@代表“Objective-C”的标志,证明您正在使用Objective-C语言 Objective-C语言关键词,@property与@synthesize配对使用. 功能:让编译好器自动编写一个与 ...
- @property和@synthesize的特性
基础回顾:get方法和set方法 定义类成员变量时,可以在@interface中定义,也可以在@implementation中定义: 在@interface中声明,成员变量的状态是受保护的,即“@pr ...
- iOS 详细解释@property和@synthesize关键字
/** 注意:由@property声明的属性 在类方法中通过下划线是获取不到的 必须是通过 对象名.属性 才能获取到!- @property和@synthesize关键字是针对成员变量以及get/se ...
- OC中两个关键字的作用:@property和@synthesize
两个关键字的使用:@property和@synthesize 一.@property关键字这个关键字是OC中能够快速的定义一个属性的方式,而且他可以设置一些值,就可以达到一定的效果,比如引用计数的问题 ...
- OC语法5——@property和@synthesize
@property和@synthesize: 我们回想一下: 在OC中我们定义一个Student类需要两个文件Student.h 和 Student.m. Student.h(声明文件):定义成员变量 ...
- Objective-C基础笔记(2)@property和@synthesize
先贴出使用@property和@synthesize实现的上一篇中的代码,再解释这两个keyword的使用方法和含义,代码例如以下: Person.h文件 #import <Foundation ...
- Objective-c @property和@Synthesize
在Objective-c中,使用@property来标识属性(一般是实例变量).在实现文件中使用@synthesize标识所声明的变量,让系统自动生成设置方法和获取方法. 也就是说@property和 ...
- @property、@synthesize和dynamic的用法
原文: http://blog.csdn.net/hherima/article/details/8622948 @代表“Objective-C”的标志,证明您正在使用Objective-C语言 O ...
- ios中点语法、property跟synthesize用法
一:OC中得点语法 1> 点语法的基本使用: ·使用 对象.成员变量 可以实现设置成员变量值,和获取成员变量的值 2> 点语法的本质 (点语法是Xcode编译器自己帮我们完成的一个 ...
- (iOS)关于@property和@synthesize的理解(原创)
开始学习ios的时候,就对一些objc的语法不理解,就比如@property和@synthesize,之前都是记住然后照着用,但是写的代码多了,对objc和ios有了一些理解,再加上最近用MRC,所以 ...
随机推荐
- SqlCommand.ExecuteScalar 方法
本文来自:http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.100 ...
- 使用ObjectAnimator开发打开、关闭书本动画
动画效果 动画效果-分享链接 (想做成gif图的,尝试各种工具无果) ObjectAnimator简单介绍及实现思路 ObjectAnimator是从api level 11 (Android3.0x ...
- leetcode_question_125 Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【MFC学习笔记-作业10-稍微难一点的画图】
设计如图所示的菜单,并编写相应用的消息处理函数.. 菜单1: 画图 画线 画矩形 画图 菜单2:颜色 红色(r) CTRL+F2; 绿色(g) CTRL +F1 蓝色(b) CTRL+F3: 问题:V ...
- 小心ThreadLocal的陷阱
ThreadLocal用在多线程时保存线程级的局部变量,当我们需要在线程内共享数据时,ThreadLocal屡试不爽,但是ThreadLocal也会有一个问题,当你使用线程池时,线程可能会被重用,所以 ...
- C# Winform下载文件并显示进度条
private void btnDown_Click(object sender, EventArgs e) { DownloadFile("http://localhost:1928/We ...
- 使用Devexpress中的CharControl控件,需要控制AxisY轴的显示范围,需要使用该控件的BoundDataChanged事件
一.控制ChartControl的Y轴范围 使用Devexpress中的CharControl控件,需要控制AxisY轴的显示范围,需要使用该控件的BoundDataChanged事件,具体代码如下: ...
- Map笔记总结
Map :存储的是键值对,一对一对出现的,要保证键的唯一性. Map常见的三个子类.1.HashTable 底层是哈希表结构,不能存在null的情况.该集合是线程同步的.效率低此类实现一个哈希表,该哈 ...
- T-SQL索引
索引 使用索引可以提高查询速度,不是越多越好,会损耗存储空间.应用于出现在where子句中的列建立索引.可以使用sql server 内置工具Profiler捕捉在SQL Server实例上执行的活动 ...
- javascript模式——Prototype模式
GoF权威的解释是,原型模式是一种通过对一个对象的克隆,创建基于这个对象的多种对象的模式. 为了实现这种原型模式,可以直接使用ECMAScript 5 中的方法Object.create.它不紧可以创 ...