main.m

#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;

}

 
Student.h

#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

 
Student.m

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

  1. Objective-C中的@property和@synthesize用法

    @代表“Objective-C”的标志,证明您正在使用Objective-C语言 Objective-C语言关键词,@property与@synthesize配对使用. 功能:让编译好器自动编写一个与 ...

  2. @property和@synthesize的特性

    基础回顾:get方法和set方法 定义类成员变量时,可以在@interface中定义,也可以在@implementation中定义: 在@interface中声明,成员变量的状态是受保护的,即“@pr ...

  3. iOS 详细解释@property和@synthesize关键字

    /** 注意:由@property声明的属性 在类方法中通过下划线是获取不到的 必须是通过 对象名.属性 才能获取到!- @property和@synthesize关键字是针对成员变量以及get/se ...

  4. OC中两个关键字的作用:@property和@synthesize

    两个关键字的使用:@property和@synthesize 一.@property关键字这个关键字是OC中能够快速的定义一个属性的方式,而且他可以设置一些值,就可以达到一定的效果,比如引用计数的问题 ...

  5. OC语法5——@property和@synthesize

    @property和@synthesize: 我们回想一下: 在OC中我们定义一个Student类需要两个文件Student.h 和 Student.m. Student.h(声明文件):定义成员变量 ...

  6. Objective-C基础笔记(2)@property和@synthesize

    先贴出使用@property和@synthesize实现的上一篇中的代码,再解释这两个keyword的使用方法和含义,代码例如以下: Person.h文件 #import <Foundation ...

  7. Objective-c @property和@Synthesize

    在Objective-c中,使用@property来标识属性(一般是实例变量).在实现文件中使用@synthesize标识所声明的变量,让系统自动生成设置方法和获取方法. 也就是说@property和 ...

  8. @property、@synthesize和dynamic的用法

    原文:  http://blog.csdn.net/hherima/article/details/8622948 @代表“Objective-C”的标志,证明您正在使用Objective-C语言 O ...

  9. ios中点语法、property跟synthesize用法

    一:OC中得点语法 1> 点语法的基本使用: ·使用 对象.成员变量   可以实现设置成员变量值,和获取成员变量的值   2> 点语法的本质 (点语法是Xcode编译器自己帮我们完成的一个 ...

  10. (iOS)关于@property和@synthesize的理解(原创)

    开始学习ios的时候,就对一些objc的语法不理解,就比如@property和@synthesize,之前都是记住然后照着用,但是写的代码多了,对objc和ios有了一些理解,再加上最近用MRC,所以 ...

随机推荐

  1. 全国计算机等级考试二级教程-C语言程序设计_第3章_顺序结构

    1输入两个整数给变量x和y:然后输出x和y:在交换x和y中的值后,在输出x和y. #include <stdio.h> main() { int x, y, t; printf(" ...

  2. Ice_cream’s world III(prime)

    Ice_cream’s world III Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Othe ...

  3. Android通过HTTP POST带參訪问asp.net网页

    在看了网络上非常多视频关于android通过HTTP POST或者GET方式訪问网页并获取数据的方法. 自己也copy了一份来測试.并通过C#.NET搭建了一个简单的后台,但发现传參时,依照网上的方式 ...

  4. Swift版音乐播放器(简化版),swift音乐播放器

    这几天闲着也是闲着,学习一下Swift的,于是到开源社区Download了个OC版的音乐播放器,练练手,在这里发扬开源精神, 希望对大家有帮助! 这个DEMO里,使用到了 AudioPlayer(对音 ...

  5. IE6下a标签上发送ajax请求总是error

    IE6下真是处处是坑啊!!!走过了一个又一个坑,记录一下吧. 之前不知道a标签上注册click事件之后,发送ajax请求总是error.后来经过几番网上搜索,终于找到高人遇到此坑的解决办法.原来是a标 ...

  6. FormData实现文件上传实例

    单提交,文件上传是一个常用又十分麻烦的功能,以前要上传文件通常都是借助插件或者flash来实现,噼里啪啦的加载一大堆东西.自从有了HTML5的FormData后,老板再也不用担心我的上传了. Form ...

  7. 在Activity中动态设置TextView的隐藏属性

    if (true) { //显示 viewHolder.tvLine.setVisibility(View.INVISIBLE);} else { //不显示 viewHolder.tvLine.se ...

  8. SQL Server 事务嵌套

    示例代码: DECLARE @TranCounter INT; SET @TranCounter = @@TRANCOUNT; -- Procedure called when there is -- ...

  9. hbase importtsv

    hadoop jar hbase-server-0.98.1-cdh5.1.3.jar importtsv -Dimporttsv.columns=HBASE_ROW_KEY,cf:imsi,cf:i ...

  10. EF6 Codefirst+MySql 数据库迁移

    简介 项目使用MSSql作为数据库,但是因为SQL服务器贵那么一点,并发连接差那么一点,要把数据迁移到MySQL,顺带迁移过程以及问题. 环境 Visual Studio 2013 MySQL 5.7 ...