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. 开源 iOS 项目分类索引大全

    GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助 系统基础库 Category/Util sstoolkit 一套Category类型的库,附带很多自定义 ...

  2. CentoS6.x网络配置

    一.配置文件 在CentoS系统里面,跟网络有关的主要配置文件有    1./etc/host.conf # 配置域名服务客户端的控制文件 2./etc/hosts # 配置主机名映射为IP的功能 3 ...

  3. 【数学水题】【TOJ4113】【 Determine X】

    题目大意: yuebai has a long sequence of integers A1,A2,-,AN. He also has such a function: F(x)=∑i=1N(⌊Ai ...

  4. asp.net 页面跳转的方法

    目前知道有4种: 1超链接 2.response.redirect("urlString") 3.server.transfer("urlString") 4. ...

  5. 如何调教Android Studio-Windows安装AS后的必备工作

    未完待续... 工欲善其事必先利其器,你已经抛弃被大众诟病的Eclipse投入Google亲儿子Android Studio的怀抱,可是不了解As的脾气,怎么让它服服帖帖的为提高开发效率做贡献呢. 关 ...

  6. Oracle的关于建表,约束,查询等的练习

    从建立一个简单表,到实现一些复杂查询的例子, DROP TABLE grade;DROP TABLE item;DROP TABLE sporter;CREATE TABLE sporter( spo ...

  7. Java SE 8 for the Really Impatient读书笔记——Java 8 Lambda表达式

    1. lambda表达式的语法 lambda表达式是一种没有名字的函数,它拥有函数体和参数. lambda表达式的语法十分简单:参数->主体.通过->来分离参数和主体. 1.1 参数 la ...

  8. Python Challenge

    0. 2的38次方 print 2**38 ##apply the result to the url 1. 看图是要right shift两位, 切片即可. import string intab ...

  9. Laravel中的队列处理

    Laravel中的队列处理 队列介绍 为什么要有消息队?这里先对其进行一个简单的介绍,方便还不了解的同学理解.在面向对象里,有一个很简单的概念--消息传递,而消息队列就可以在它上面扩展一下,把它说的更 ...

  10. python排序(冒泡, 快速)

    之前用java时学习的一些基础算法,今天在python上也研究下. 1. 冒泡排序 算法步骤: 50   30   70  90 10 1)50 跟 30 比不用交换. 2)步数+1, 30 跟70比 ...