课堂笔记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.
//
int main(int argc, const char * argv[]) {
@autoreleasepool {
// NSLog(@” name is %@”,[st name]);
// NSLog(@”%ld”,[st age]);
//NSLog(@”My name is %@”,[st name]);
[st sayHello];
// Student.h
// OC_02
//
// Created by Ibokan on 15/7/23.
// Copyright (c) 2015年 Law. All rights reserved.
//
@interface Student : NSObject{
//若改为public,会将类的封装性给破坏了
NSString *_name;
NSInteger _age;
}
@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.
//
@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.
//
@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.
//
@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.
//
@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
这一节将的是关于设置器以及访问器的内容
//
// 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
相关文章推荐
- NSOperation - 02
- iOS中的一些小知识点
- 提高网站打开速度的7大秘籍
- 关于友盟分享
- Mysql + LVS + KeepAlived 主主同步
- POJ2031,Building a Space Station,最小生成树模版题
- android 永久NotitleBar
- 声明
- 北漂两星期的感悟
- Networking---poj1287最小生成树
- 2.09-sizeof
- Android中的三种XML解析方式的差异和比较
- 命令执行分隔符辨析
- poj 2104 and hdu 2665 划分树模板入门题
- C# NET DataTable转Excel 并 下载
- Windows 与开发板之间使用tftp服务不通的问题
- matlab 图片批量读取
- 命令执行分隔符辨析
- PUT vs POST in REST
- Android bitmap高效显示和优化