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

先贴出使用@property和@synthesize实现的上一篇中的代码,再解释这两个关键字的用法和含义,代码如下:

Person.h文件

#import <Foundation/Foundation.h>

@interface Person : NSObject {
    int _age;  //可以被子类访问
    //这里系统会帮我们生成一个默认的 int _no 私有变量(不能被子类访问)
}

@property int age;
@property int no;

//自己写一个构造方法
- (id)initWithAge:(int)age andNo:(int)no;

@end

Person.m文件

#import "Person.h"

@implementation Person

//Xcode 4.5以上都不用写下面两句(可省略,并且默认是_age和_no)
//@synthesize age = _age; //声明为protected
//@synthesize no = _no; //默认生成的是私有的

- (id)initWithAge:(int)age andNo:(int)no {
    if(self = [super init]){
        _age = age;
        _no = no;
    }
    return self;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"age is %i and no is %i", _age, _no];
}

@end

main.m文件

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *person = [[Person alloc] initWithAge:15 andNo:2];

        NSLog(@"age is %i and no is %i", person.age, person.no);

        [person setNo:3];
        NSLog(@"no is %i", [person no]);
        //%@代表打印一个OC对象
        NSLog(@"%@", person);
    }
    return 0;
}

输出结果:

2014-11-12 21:53:15.406 firstOCProj[826:47802] age is 15 and no is 2

2014-11-12 21:53:15.407 firstOCProj[826:47802] no is 3

2014-11-12 21:53:15.408 firstOCProj[826:47802] age is 15 and no is 3

可以看到上面的代码简洁了不少,@property的作用就等价于对成员变量的声明,@synthesize的作用就等价于对成员变量setter和getter的标准实现。

需要注意的是:

1、在Xcode4.5以上可以不用写@synthesize属性(编译器默认添加)。

2、这种写法可以和前面的写法(旧的写法)交叉使用。

3、如果要对setter或者getter方法有特殊处理,可以使用旧的写法(编译器就不会默认帮我们实现)。

4、如果没有显式的声明变量,则默认生成一个私有成员变量(不能被子类使用,如上面的_no)。

下面我们将上面代码该的更简单一些:

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property int age;
@property int no;

@end

Person.m

#import "Person.h"

@implementation Person

@end

main.m

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *person = [Person new];
        [person setAge:20];
        [person setNo:3];
        NSLog(@"age is %i and no is %i", person.age, person.no);
    }
    return 0;
}

输出结果:

2014-11-12 22:14:44.002 firstOCProj[849:52867] age is 20 and no is 3

注意:我的编译器Xcode是4.5以上的,所以能省略Person.m中的@synthesize属性。

这里不得不对OC和Xcode编译器感叹,如此方便和好用的工具我真心是第一次见,默默的赞一下。

时间: 2024-08-05 07:46:08

Objective-C基础笔记(2)@property和@synthesize的相关文章

黑马程序员_OC学习笔记之@property和@synthesize

[objc] view plaincopyprint? <span style="font-size:24px;">#import <Foundation/Foundation.h> @interface Person : NSObject { int _age; int age; int _height; int height; int _weight; int weight; int _money; int money; } @property int ag

[Objective-c 基础 - 2.6] @property和@synthesize

Xcode编译器的特性,自动生成getter和setter [email protected] 自动生成某个成员变量的getter和setter的声明 变量的命名要求:以下划线开头 1 2 Student.h 3 @interface Student : NSObject 4 { 5 int _age; 6 int _no; 7 int age;//此变量不会被访问到 8 double height; 9 NSString *_name; 10 } 11 12 @property int age

黑马程序员---Objective-C基础学习---编译器特性@property和@synthesize

------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 编译器特性@property和@synthesize 1.@property @property可以自动生成某个成员变量的setter和getter声明. 新建一个项目,添加Person类. Person.h // // Person.h // zijia // // Created by zou on 5/10/15. // Copyright (c) 2015 __MyCompanyNam

OC方面的基础笔记摘录

OC方面的基础笔记摘录: 1.类的基本用法 #import <Foundation/Foundation.h>// 大体上就是include, 用于包含头文件, 但是即使头文件中, 没有ifndef defined endif, 仍然能够踢除重复包含的头文件// [email protected] section----// OC中声明和实现是分离的, 两个都必须有.@interface Fraction : NSObject {    // @interface 类名: 父类名字// 在这里

@synthesize obj=_obj的意义详解 @property和@synthesize

本文转载至 http://blog.csdn.net/ztp800201/article/details/9231969 http://hi.baidu.com/feng20068123/item/ca8952fa661e5342932af2c2 写的非常不错,攒一个!!!! 我们在进行iOS开发时,经常会在类的声明部分看见类似于@synthesize window=_window; 的语句,那么,这个window是什么,_ window又是什么,两个东西分别怎么用,这是一个比较基本的问题,也关

C#基础笔记---浅谈XML读取以及简单的ORM实现

背景: 在开发ASP.NETMVC4 项目中,虽然web.config配置满足了大部分需求,不过对于某些特定业务,我们有时候需要添加新的配置文件来记录配置信息,那么XML文件配置无疑是我们选择的一个方案之一.下面简单谈谈XML的读取.  一. xml.linq读取xml 1.新建一个data.XML文件 1 <Customers> 2 <Customer> 3 <Name>Frank</Name> 4 <City>成都</City>

iphone开发-基础笔记总结(1)

1.ios完整学习路线 2.ios开发的一般步骤: 搭建界面                                         UI界面(User Interface) 发送网络请求                                   多线程/网络 网络数据解析                                   json/xml解析 在界面上进行数据展示                   数据的封装展示 3.为了方便开发者开发出强大的功能,苹果提

黑马程序员---OC---点语法、属性作用域、@property与@synthesize、id、构造方法、分类

------iOS培训.Java培训.Android培训, iOS学习型技术博客,期待与您交流------                                    点语法 点语法的本质是方法调用:调用对象成员变量的setter和getter 是编译器特性,编译器帮忙转的 p.age = 25;       // 等价于 [p setAge:25]; int a = p.age;     // 等价于 int a = [p age]; // 访问成员变量不能用点语法,而是 p->_a

Objective-C入门教程03:属性(@property和@synthesize)

在Java中,特别是一个标准的POJO类,我们定义了一些属性,然后针对每个属性生成相应的getter和setter.例如: package com.demo; /** * 手机类 * @author liuzc */ public class Phone { private String color; //颜色 private String os; //系统 private String brand; //品牌 /******* Getter & Setter *******/ public S