Objective-C - @property与@synthesize

@property与@synthesize

@interface Person : NSObject
{
    int _age;
    // int age;

    int _height;

    double _weight;

    NSString *_name;
}

// @property:可以自动生成某个成员变量的setter和getter声明
@property int age;
//- (void)setAge:(int)age;
//- (int)age;

@property int height;
//- (void)setHeight:(int)height;
//- (int)height;

- (void)test;

@property double weight;

@property NSString *name;

@end
#import "Person.h"

@implementation Person

// @synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量
@synthesize age = _age;

@synthesize height = _height;

@synthesize weight = _weight, name = _name;

@end

车类

#import <Foundation/Foundation.h>

@interface Car : NSObject
{
    //int _speed;
    //int _wheels;
}
@property int speed;
@property int wheels;

//@property int speed, wheels;
- (void)test;
@end
#import "Car.h"

@implementation Car
//@synthesize speed = _speed, wheels = _wheels;
// 会访问_speed这个成员变量,如果不存在,就会自动生成@private类型的_speed变量
@synthesize speed = _speed;
@synthesize wheels = _wheels;

- (void)test
{
    NSLog(@"速度=%d", _speed);
}

@end

狗类

@interface Dog : NSObject
@property int age;
@end
#import "Dog.h"

@implementation Dog

- (void)setAge:(int)age
{

}

//- (int)age
//{
//
//}

//- (int)age
//{
//    return 10;
//}

@end

猫类

@interface Cat : NSObject
{
    //int _age;
    //int age;
}
@property int age;
@end
@implementation Cat

// 默认会访问age这个成员变量,如果没有age,就会自动生成@private类型的age变量
@synthesize age;

@end
时间: 2024-08-01 10:45:23

Objective-C - @property与@synthesize的相关文章

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

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; //自己写一个构造方法 - (

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

@代表“Objective-C”的标志,证明您正在使用Objective-C语言 Objective-C语言关键词,@property与@synthesize配对使用. 功能:让编译好器自动编写一个与数据成员同名的方法声明来省去读写方法的声明. 如: 1.在头文件中: C代码   @property int count; 等效于在头文件中声明2个方法: C代码   - (int)count; -(void)setCount:(int)newCount; 2.实现文件(.m)中 C代码   @sy

黑马程序员_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基础学习---编译器特性@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语法5——@property和@synthesize

@property和@synthesize: 我们回想一下: 在OC中我们定义一个Student类需要两个文件Student.h 和 Student.m. Student.h(声明文件):定义成员变量,并且为了使外界可以访问操作这些成员变量,需要定义set和get方法提供给外界.最后还要定义自定义的功能方法. Student.m(实现文件):实现文件实现set和get方法,并且实现自定义的功能方法. 假如,Student类中有很多个成员变量,那我们若手动定义成员变量,再一个一个声明它们的set和

OC中点语法、property跟synthesize用法

一:OC中得点语法 1> 点语法的基本使用: ·使用 对象.成员变量   可以实现设置成员变量值,和获取成员变量的值 2> 点语法的本质 (点语法是Xcode编译器自己帮我们完成的一个功能) 实际上点语法就是set和get方法,当编译遇到点语法时,编译器就会将点语法转成set和get方法. 注意:也就是说,如果类中没有实现set和get方法,那么就不会有点语法 p.name = @“”; 实际上就是 [p setName:@“”]; 在OC中访问成员变量只有一种方式即使用-> 如stu-

@property、@synthesize和dynamic的用法

原文:  http://blog.csdn.net/hherima/article/details/8622948 @代表“Objective-C”的标志,证明您正在使用Objective-C语言 Objective-C语言关键词,@property与@synthesize配对使用. 功能:让编译器自动编写一个与数据成员同名的方法声明来省去读写方法的声明. 如: 1.在头文件中: @property int count; 等效于在头文件中声明2个方法,即通常说的GetXXX  SetXXX. -

IOS第三天(@property与@synthesize的用法)

一.@property与@synthesize基本规范用法 [email protected] 当编译器遇到@property时,会自动展开成getter和setter的声明 #import <Foundation/Foundation.h> @interface Student : NSObject { int _age; int _no; float _height; } // 当编译器遇到@property时,会自动展开成getter和setter的声明 @property int ag

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

开始学习ios的时候,就对一些objc的语法不理解,就比如@property和@synthesize,之前都是记住然后照着用,但是写的代码多了,对objc和ios有了一些理解,再加上最近用MRC,所以在各种内存检测和变量使用的过程中,探讨了一下,最终对它们的作用有了一定了解. 一般@property和@synthesize是搭配使用的,用@property声明的变量,系统默认给他们做了setter和getter处理. property可以声明属性的各种属性. 1.声明属性的访问方法: gette