简单一句话:Object c的点语法是在建立在有get 和set 的基础上面的,可以实现和java的点语法类似的,直接调用,而不用——》来调用了
person.h
//
// Person.h
// DotDemo
//
// Created by 千 on 16/9/13.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
@public
NSString *_name;
CGFloat _age;
}
-(void)setName:(NSString *)newName;
-(void)setAge:(CGFloat)newAge;
-(CGFloat)age;
-(NSString *)name;
@end
person.m
//
// Person.m
// DotDemo
//
// Created by 千 on 16/9/13.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import "Person.h"
@implementation Person
-(void)setName:(NSString *)newName{
_name = newName;
}
-(void)setAge:(CGFloat)newAge{
_age = newAge;
}
-(NSString *)name{
return _name;
}
-(CGFloat)age{
return _age;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name=%@,age=%f",_name,_age];
}
@end
main.m
//
// main.m
// DotDemo
//
// Created by 千 on 16/9/13.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
Person *person=[Person new];
//点语法的使用的前提是必须要有get和set方法
person.name=@"hello ";
person.age = 4.5f;
NSLog(@" name =%@",person.name) ;
NSLog(@"%@",person);
}
return 0;
}