OC开发之——@property和@synthesize(26)

本文详细介绍了Objective-C中@property和@synthesize的使用方法,包括它们如何简化setter和getter的声明与实现,以及如何省略成员变量的写法。通过具体实例,展示了属性在类定义中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 概述

  • @property 可以在类.h文件中自动生成某个成员变量的setter和getter的声明
  • @synthesize 可以在类.m文件中自动生成某个成员变量的setter和getter的实现
  • @property 和@synthesize是xcode为了简化类的书写,而出现的新特性

二 作用演示

2.1 Person.h头文件定义(@property)

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    int _age;
    int age;
    int _height;
    double _weight;
    NSString *_name;
}
@property int age;
@property int height;
@property double weight;
@property NSString *name;

-(void)test;
@end

2.2 Person.m文件定义(@synthesize)

#import "Person.h"

@implementation Person
@synthesize age=age;
@synthesize height=_height;
@synthesize weight=_weight;
@synthesize name=_name;

-(void)test
{
    NSLog(@"age=%d,_age=%d",age,_age);
}
@end

2.3 main.m入口文件调用

 Person *person=[Person new];
 [person setAge:10];
 [person test];

2.4 总结

2.4.1 @property
  • 用在@interface中

  • 用来自动生成setter和getter的声明

  • 用@property int age;就可以替代下面的两行

    -(void)setAge:(int)age;//setter
    -(int)age; //getter
    
2.4.2 @synthesize
  • 用在@implementation中

  • 用来自动生成setter和getter的实现

  • 用@synthesize age=_age;就可以代替

    -(void)setAge{_age=age;};
    -(int)age}{return _age};
    

三 省略成员变量的写法

3.1 声明与实现

//声明
#import <Foundation/Foundation.h>
@interface Car : NSObject
@property int speed;
@property int wheels;
@end

//实现
#import "Car.h"
@implementation Car
@synthesize speed=_speed;
@synthesize wheels=_wheels;
@end

3.2 方法调用

  Car *car=[Car new];
  car.speed=100;
  NSLog(@"Car的速度是%d",car.speed);

3.3 说明

  • 实现中@synthesize speed=_speed;会访问_speed这个成员变量,如果不存在,就会自动生成@private类型的_speed变量

四 @property替代@property和@synthesize的用法

4.1 代码(Dog.h,不修改Dog.m文件)

#import <Foundation/Foundation.h>
@interface Dog : NSObject
@property int age;
@end

4.2 文件调用(正常输出)

 Dog *dog=[Dog new];
 dog.age=5;
 NSLog(@"Dog的年龄是%d",dog.age);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值