@fiy-fish
2015-07-18T20:15:40.000000Z
字数 925
阅读 1312
Objective-c
// day07-02-点语法
//
// Created by Aaron on 15/7/9.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
//点语法
//.
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *p = [[Person alloc] init];
//[p setName:@"xiaobai"];
//oc加入点语法,原始的初衷是为了让程序员更方便的访问属性
p.name = @"xiaobai";
//NSLog(@"%@",[p name]);
NSLog(@"%@",p.name);
//[p askToOther:p];
p.askToOther = p;
//在等号的左边,表示要使用设值方法
// set+AskToOther
[p setAskToOther:p];
//在=右边或者没有=
//直接调用 age 方法
NSLog(@"%ld",p.age);
//OC的点语法是帮我们访问setter getter接口
//OC的点语法和属性无关
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString *_name;
}
-(void)setName:(NSString *)name;
-(NSString *)name;
-(void)setAskToOther:(Person *)p;
-(NSInteger)age;
@end
#import "Person.h"
@implementation Person
-(void)setName:(NSString *)name
{
if(_name != name)
{
_name = name;
}
}
-(NSString *)name
{
return _name;
}
-(void)setAskToOther:(Person *)p
{
NSLog(@"........");
}
-(NSInteger)age
{
return 100;
}
@end