您的位置:首页 > 其它

课堂笔记02

2015-07-27 09:59 429 查看
####课堂笔记02###

这一节将的是关于设置器以及访问器的内容

//

// main.m

// OC_02

//

// Created by Ibokan on 15/7/23.

// Copyright (c) 2015年 Law. All rights reserved.

//

#import <Foundation/Foundation.h>
#import "Student.h"
#import "NewStudent.h"
#import "NewNewStudent.h"


int main(int argc, const char * argv[]) {

@autoreleasepool {

Student *st = [Student new];

[st setCompanyAddress:@"黄埔大道东"];
//st.companyAddress = @"黄埔大道西";
NSLog(@"%@",[st companyAddress]);

[st setName:@"窦你玩"];
[st setAge:18];


// NSLog(@” name is %@”,[st name]);

// NSLog(@”%ld”,[st age]);

//NSLog(@”My name is %@”,[st name]);

[st sayHello];

Student *st1 = [Student new];
[st1 sayHi];

//NSLog(@"Hello, World!");

NewStudent *newST = [NewStudent new];
newST.name = @"挨踢";
newST.age = 15;
NSLog(@"name is %@,age is %ld",newST.name,newST.age);
NewNewStudent *nnst =[[NewNewStudent alloc] init];
nnst.name = @"攻城狮";
NSLog(@"%@",nnst.name);

}
return 0;

//


// Student.h

// OC_02

//

// Created by Ibokan on 15/7/23.

// Copyright (c) 2015年 Law. All rights reserved.

//

#import <Foundation/Foundation.h>


@interface Student : NSObject{

//若改为public,会将类的封装性给破坏了

NSString *_name;

NSInteger _age;

NSString *_companyAddress ;


}

@property NSString *companyAddress;

//相当于设置了设置器和访问器

//-(void)setCompanyAddress:(NSString *)companyAddress;

//-(NSString *)companyAddress;

@property NSInteger age;

-(void)sayHi;

-(void)sayHello;

/*

设置器以及访问器的作用

1、隐藏了实例变量

2、控制实例变量的读写

3、做正确性检验或校验

对于设置器和访问器来说,其命名规则:

1.设置器: -(void)set+首字母大写的实例变量名:(实例变量的返回值的类型)去掉下划线的实例变量名

2.访问器: -(实例变量的返回值类型)去掉下划线的实例变量名

note :

对于类的名称,其首字母大写;

对于变量,其首字母小写;

遵循骆驼峰命名规则

缺点:

手动编写麻烦

*/

//设置器和访问器

//对于name属性来说

-(void)setName:(NSString *)name;

-(NSString *)name;

//对于age属性来说

-(void)setAge:(NSInteger)age;

-(NSInteger)age;

@end

//

// Student.m

// OC_02

//

// Created by Ibokan on 15/7/23.

// Copyright (c) 2015年 Law. All rights reserved.

//

#import "Student.h"


@implementation Student

//@synthesize name,age;//相当于设置器和访问器的实现

/*

-(void)setName:(NSString *)name{

_name = name;

}

*/

@synthesize companyAddress = _companyAddress;

-(id)init{

if (self = [super init]) {

_name = @”小学��”;

_age = 18;

}

return self;

}

-(void)sayHi{

NSLog(@”My name is %@,I am %ld years old”,_name,_age);

}

-(void)setName:(NSString *)name{

_name = name;

}

-(NSString *)name{

return _name;

}

-(void)setAge:(NSInteger)age{

if (age < 18) {

_age = 18;

}

else{

_age = age;

}

}

-(NSInteger)age{

return _age;

}

-(void)sayHello{

NSLog(@”My name is %@,%ld years old”,_name,_age);

}

@end

//

// NewStudent.h

// OC_02

//

// Created by Ibokan on 15/7/24.

// Copyright (c) 2015年 Law. All rights reserved.

//

#import <Foundation/Foundation.h>


@interface NewStudent : NSObject

{

NSString *_name;

NSInteger _age;

}

@property NSString *name;

@property (nonatomic,assign)NSInteger age;

//任何一个继承与 NSObject 都有一个方法

/*

-(id)init{

self = [super init];//初始化父类的方法

if(self){

_name = @”程序��”;

}

return self;

}

*/

@end

//

// NewStudent.m

// OC_02

//

// Created by Ibokan on 15/7/24.

// Copyright (c) 2015年 Law. All rights reserved.

//

#import "NewStudent.h"


@implementation NewStudent

@synthesize name = _name;

@synthesize age = _age;

//控制

-(void)setAge:(NSInteger)age{

NSLog(@”你调用了我,%s”,FUNCTION);

if (age < 18) {

_age = 18;

}

else{

_age = age;

}

}

@end

//

// NewNewStudent.h

// OC_02

//

// Created by Ibokan on 15/7/24.

// Copyright (c) 2015年 Law. All rights reserved.

//

#import <Foundation/Foundation.h>


@interface NewNewStudent : NSObject

{

}

@property (nonatomic,retain)NSString *name;

@property (nonatomic,assign)NSInteger age;

//@property (访问控制,原则性,内存管理) nonatomic(非原子操作) atomic(原子操作)

/*

retain //

copy

assign //不能用于对象

//ARC环境下的关键字

strong

weak //不能用于对象

*/

@end

//

// NewNewStudent.m

// OC_02

//

// Created by Ibokan on 15/7/24.

// Copyright (c) 2015年 Law. All rights reserved.

//

#import “NewNewStudent.h”

@implementation NewNewStudent

//@synthesize name = _name;

//@synthesize age = _age;

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: