点语法
.h--》.m--》.m
——————————————————————分割———————————————————————————————
#import <Foundation/Foundation.h>
@interface Dog : NSObject
{
NSString *_name;
NSInteger _age;
}
- (id)initWithName:(NSString *)name andAge:(NSInteger)age;
//setter方法
- (void)setName:(NSString *)name;
- (void)setAge:(NSInteger)age;
//getter方法
- (NSString *)name;
- (NSInteger)age;
@end
#import "Dog.h"
@implementation Dog
- (id)initWithName:(NSString *)name andAge:(NSInteger)age
{
if (self = [super init]) {
_name = name;
_age = age;
}
return self;
}
- (void)setName:(NSString *)name
{
_name = name;
}
- (void)setAge:(NSInteger)age
{
_age = age;
}
- (NSString *)name
{
return _name;
}
- (NSInteger)age
{
return _age;
}
@end
#import <Foundation/Foundation.h>
#import "Dog.h"
//点语法
//方便别的程序员转到ios开发行业
//struct stu
//{
// char name[20];
// int age;
//};
//struct stu student;
//student.age;
//student.name;
//点语法是编译层次的概念
//程序在编译后, 自动展开成经典调用方式
int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *xiaoHei = [[Dog alloc] initWithName:@"小黑" andAge:12];
//NSLog(@"name = %@ , age = %ld", [xiaoHei name], [xiaoHei age]);
//xiaoHei.name xiaoHei.age
//展开成 ==>[xiaoHei name] [xiaoHei age];
NSLog(@"name = %@ , age = %ld", xiaoHei.name, xiaoHei.age);
//xiaoHei.name = @"小白"
//xiaoHei.age = 13;
//==> [xiaoHei setName:@"小白"];
//==> [xiaoHei setAge:13];
xiaoHei.name = @"小白";
xiaoHei.age = 13;
NSLog(@"name = %@ , age = %ld", xiaoHei.name, xiaoHei.age);
}
return 0;
}
.h--》.m--》.m
——————————————————————分割———————————————————————————————
#import <Foundation/Foundation.h>
@interface Person : NSObject
//{
// NSString *_name;
// NSInteger _age;
//}
//- (void)setName:(NSString *)name;
//- (void)setAge:(NSInteger)age;
//
//- (NSString *)name;
//- (NSInteger)age;
//@property 自动帮我们展开方法的声明
@property NSString *name;
@property NSInteger age;
@end
#import "Person.h"
@implementation Person
//- (void)setName:(NSString *)name
//{
// _name = name;
//}
//- (void)setAge:(NSInteger)age
//{
// _age = age;
//}
//
//- (NSString *)name
//{
// return _name;
//}
//- (NSInteger)age
//{
// return _age;
//}
//@synthesize 展开setter 及 getter方法的实现
//@synthesize name=_name;
//@synthesize age=_age;
@end
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *xiaoHong = [[Person alloc] init];
//调用setter 方法
xiaoHong.name = @"小红";
xiaoHong.age = 12;
//
//NSLog(@"name = %@ age = %ld", xiaoHong.name, xiaoHong.age);
NSLog(@"name = %@ age = %ld", [xiaoHong name], [xiaoHong age]);
}
return 0;
}
.h--》.m--》.m
——————————————————————分割———————————————————————————————
#import <Foundation/Foundation.h>
//readonly
//只展开getter方法, 不展开setter方法
//readwrite(缺省)
//既展开了getter方法, 又展开了setter方法
//内存管理中讲
//retain(缺省) //setter方法的实现不同
//copy
//atomic(缺省) 原子性的
//nonatomic 非原子性
//setter getter
//方法名不能以 new copy alloc mulCopy开头
//setter给setter方法命名
//getter给getter方法命名
@interface Dog : NSObject
@property (readwrite,copy)NSString *name;
@property (setter=masName:,getter=getName)NSString *masterName;
@property (nonatomic)NSInteger age;
//@property (getter=newPlace)NSString *place;
@end
#import "Dog.h"
@implementation Dog
@end
#import <Foundation/Foundation.h>
#import "Dog.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *xiaoHei = [[Dog alloc] init];
xiaoHei.name = @"小黑";
xiaoHei.age = 12;
NSLog(@"name = %@ age = %ld", xiaoHei.name, xiaoHei.age);
[xiaoHei masName:@"小飞"];
//[xiaoHei setMasterName:@"小飞"];
xiaoHei.masterName = @"小红";
NSLog(@"masterName = %@", xiaoHei.masterName);
NSLog(@"masterName = %@", [xiaoHei getName]);
}
return 0;
}
posted on 2014-03-04 18:33 Sinner_Yun 阅读(236) 评论(0) 编辑 收藏 举报