您的位置:首页 > 其它

OC的基础语法OC继承和复合语言特性目标动作回调

2015-08-30 14:38 375 查看
复习整理:
OC的基础语法:
1,类的声明:关键字是@interface @end 关键字后面跟类名和父类名,类名和父类名之间用:号隔开
@interface leiming : NSObject
@end
2, OC类的成员变量,在声明类中间用{}声明
@interface leiming :NSObject
{
//在此大括号内声明成员变量
double _a;
}
其中_a,_b,_c分别为此类的成员变量。在OC中成员变量通常以 _ 开头。
成员变量设置权限:
@public公开的
@protected受保护的
@private私有变量
其中受保护的是默认的权限
3, OC类的方法声明:
@interface leiming :NSObject
{
double _a;
double _b;
double _c;
}
-(void)setA:(double)a;
-(void)setB:(double)b;
-(void)setC:(double)c;

-(double)a;
-(double)b;
-(double )c;

-(double)area;
@end
方法类型有两种,包括+号和-号方法,加号方法称为类方法,减号方法称为对象方法。只有在便利构造器,单例,纯过程封装时用加号方法。
声明的第二步要确定返回值类型 -(返回值类型)test;
声明方法第三步确定方法名和参数 参数描述字段:(形参类型)形参名
两种特殊的方法:-(void)setA:(int )a称为setter方法-(double)a为getter方法
4,OC的实现 实现要在.m类型的文件中
@implementation leiming
例如:
@implementation Triangle
-(void)setA:(int )a;
{
_a = a;
}
-(void)setB:(int)b;
{
_b = b;
}
-(void)setC:(int)c;
{
_c = c;
}
-(double)a
{
return _a;
}
-(double)b
{
return _b;
}
5 ,OC属性机制
@property double a; 注意:声明的属性为a,但是编译时生成的成员变量为_a;
@interface Triangle: NSObject
@property double a;
@property double b;
@property double c;
-(double)area;
@end

@implementation Triangle
-(double)
{
//根据成员变量的值,计算三角形的面积
double s = (_a+_b+_c)/2
return sqrt(s*(s-_a)*(s-_b)*(s-_c));//海伦公式
}
@end
6,OC创建对象
分三步:创建对象指针 为对象指针分配内存alloc方法分配内存 init将分配的内存进行初始化
Triangle * t ;
t = [[Triangle alloc]init];
7,OC方法调用
对象创建完成后,用对象指针调用对象的方法。OC中用[]来实现
[t setA:10];//将10赋给t
double = [t area];
8,OC点语法
点语法的一个重要特性就是简化setter和getter的方法
[t setA:10];=t.a = 10;
double a1 = [t a];=double a2 =r.a;
9.self关键字
oc语言里面self关键字为方法调用时的隐含参数
10.自定义初始化方法
-(instancetype)initWithA:(double)a B:(double)b C:(double)c;
{
self = [super init];
if(self)
{
self.a = a;
self.b = b;
self.c = c;
}
return self;
}
其中instancetype为固定写法,表示返回类型有编译器自动辨别。
Triangle * t = [[Triangle alloc]initWithA:10 B:11 C:12];

OC继承和复合
1,OC继承语法
OC语法只支持单根继承,即一个类只有一个父类。
继承关键字为:
@interface 类目:父类名
@interface Triangle : NSObject
@end
OC继承规定,通过继承方式,可以继承父类的所有方法和除私有权限以外的所有成员变量。
@interface Triangle :NSObject
@property double a;
@property double b;
@property double c;
-(instancetype)initWithA:(double)a B:(double)b C:(double)c;
-(double)area;
@end
@implementation Triangle
-(instancetype)initWithA:(double)a B:(double)b C:(double)c
{
self = [super init];
if(self){
self.a = a;
self.b= b;
self.c = c;
}
return self;
}
-(double)area
{
double s = (_a+_b +_c)/2;
return art (s*(s-_a)*(s-_b)s*(s-_c));
}
@interaface TriangleSub : Triangle
@end
@implementation TriangleSub
@end
TriangleSub * subT = [[TriangleSub alloc]initWithA:10 B:11 C:12];
double s = [subT area];
NSLog(@“s = %g”,s);
2,OC语言的动态特性
@interface TestClass: NSObject
-(void)test;
@end
@implementation TsetClass
-(void)test
{
NSLog(@“这里是TestClass类的Test方法”);
}
@end
@interface SubA:TestClass
@end
@omplementation SubA
//重写父类的方法
-(void)test
{
NSlog(@“这里是SubA类的test方法”);
}
@end
SubA *a = [[SubB alloc]init];
[a test];
OC的复合模式
@interface Computer :NSObject
@property(strong,nonatomic)KeyBoard * aKeyBoard;
@property(strong,nonatomic)BigMouse * aMouse;
@property(strong,nonatomic)Monitor * aMonitor;
@property(strong,nonatomic)IntelCPU * aCPU;
@end
属性括号中的内容:属性控制符 分别对属性的读写权限,内存管理和线程操作做出相关规定。
读写权限由两个关键字控制,分别为readonly和readwrite.默认为readwrite
内存管理权限由四个关键字控制,分别为assgin,strong,weak和copy.默认为assgin。如果属性类型为基本数据类型,那么只能用assgin。如果属性类型为对象指针,一般情况下用strong,特殊情况下用weak和copy。
线程操作权限由两个关键字控制,为:atomic和nonatomic.其中默认为atomic。通常使用nonatomic作为线程操作。
@implementation Computer
-(instancetype)init
{
self = [super init];
if (self)
{
self.aKeyBoard = [[KeyBoard alloc]init];
self.aMouse = [[BigMouse alloc]init];
self.aMonitore = [[Monitor alloc]init];
aelf.aCPU = [[IntelCPU alloc]init];
}
return self;
}
@end
super 关键字
@interface Kongtiao:NSObject
-(void)zhileng;
@end
@interface Chujiaquan :NSObject
-(void)Chujiaquan;
@end
@interface NewKongtiao :Kongtiao
@property(strong,nonatomic)Chujiaquan * cJQ;
@end
@implementation NewKongTiao
-(instancetype)init
{
self = [super init];
if (self)
{
self.cJQ = [[Chujiaqian alloc]init];
}
@end
语言特性
类目,延展,协议
类目:为任何一个类添加方法
创建Student类
@interface Student : NSObject
@property (nonatamic ,strong)NSString *name;
@end
@implementation
@end
在无法获取类的源码且需要给一个类添加方法的情况下,使用类目。
创建类目:@interface Student (Test)
-(void)test;
@end
@implementation Student (Test)
-(void)test
{
NSLog(@“这里是Student类目的test方法”);
}
@end
实现:Student * s = [[Student alloc]init];
[s test];
延展;
延展可以看做一种匿名的类目,所有的私有方法都可以通过延展方式来声明。
@interface Student ()
-(void)zuobi;
@end
在Student.h文件中
@interface Student :NSObject
@property(nonatomic,strong)NSString *name;
@end
在Student.m文件中
@interface Student :NSObject
@property(nonatomic,strong)NSString*name;
@end
@implementation Student (Test)
-(void)test
{
NSLog(@“这里是Student类目的test方法”);
}
-(void)zuobi
{
NSLog(@“正在作弊”);
}
@end
协议:一组方法的声明,不需要实现。遵守协议的类负责实现协议中的方法。
协议使用@protocol实现关键字声明。@required 和@optional关键字。不写默认为必须实现的方法。
@protocol Test <NSObject>
@required
-(void)fun1;//遵守协议的类,必须实现
@optional
-(void)fun2;//可选实现
@end
-(void)test:(id<Test>)obj;obj必须遵守Test协议
@property id <Test>obj;

目标动作回调
1,回调(就是反馈)能反馈自身状态的机制。
三种表现形式:1目标-动作回调2委托模式回调3代码块回调
2,目标-动作回调
目标:接收事件反馈的对象。在代码中使用target来表示目标对象。
动作:事件反馈所要触发的方法。动作对象为SEL类型。
SEL test = @selector(方法名);selector—>选择器
能够设置反馈目标和反馈动作的组件我们称为具有目标动作回调接口的组件。
3,使用具有目标动作回调接口的组件
灯的声明文件
@interface Light :NSObject
-(void)turnOff;
-(void)turnOn;
@end
开关的声明文件
typedef enum :NSUInteger{
SwitchStateOff,
SwitchStateOn,
}SwitchState;
@interface Switch :NSObject
@property (nonatomic,assign,readonly)SwitchState currentState;
-(void)addChangsStateTarget:(id)target Action:(SEL)action;
@end
开关有一个只读属性,为当前开关的状态,其状态变量为枚举类型
SwitchStateOff 表示关闭状态
SwitchStateOn 表示开启状态
开关还有一个addChangeStateTarget方法,通过该方法为这个开关设置反馈对象和反馈动作,以实现让反馈对象受到开关状态的改变信息。
@interface Room:NSObject
@end
@interface Rom()
@property (nonatomic,strong)Light *aLight;
@property(nonatomic,strong)Switch * aSwitch;
-(void)changeState:(Switch *)s;
@end
@implementation Room
-(instancetype)init
{
self = [super init];
if(self)
{
self.aLight = [[Light alloc]init];
self.aSwitch = [[Switch alloc]init];
//设置反馈对象和反馈方法
[self.aSwitch addChangeStateTarget:self Action:@selector(changeState:)];
}
return self;
}
-(void)changeState:(Switch*)s
{
if(self.w.state == SwitchStateOff)
{
[self.light turnOn];
}
else
{
[self.light turnOff];
}
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: