OC-@property和@synthesize理解

//
// main.m
// homework_oc807
//
// Created by 邱学伟 on 15/8/12.
// Copyright (c) 2015年 邱学伟. All rights reserved.
//

//1.@property和@synthesize
#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 age;
@property int height;
@property int weight;
@property int money;

- (void)test;
@end

@implementation Person
@synthesize height = _height;
@synthesize weight;

- (void)setMoney:(int)money
{
    self->money = money;
}

- (int)height
{
    return 180;
}

- (int)age
{
    return age;
}

- (void)test
{
    NSLog(@"age=%d, _age=%d, self.age=%d", age, _age, self.age);
    NSLog(@"height=%d, _height=%d, self.height=%d", height, _height, self.height);
    NSLog(@"weight=%d, _weight=%d, self.weight=%d", weight, _weight, self.weight);
    NSLog(@"money=%d, _money=%d, self.money=%d", money, _money, self.money);
}
@end

int main()
{
    Person *p = [Person new];
    p.age = 10;
    p.weight = 50;
    p.height = 160;
    p.money = 2000;
    [p test];
    return 0;
}
/* 10,0,10 错!——》0 10 0 160,160,180 错! ---》0 160 180 50,0,50 对! 2000,0,2000 错! ---》2000 0 0 */

你可能感兴趣的:(Objective-C)