1、NSObject:
1.1、所有自定义类的根类
1.2、提供了类对象的创建方法 alloc、init、new
[SHDog new]; <=>[[SHDog alloc]init];
1.3、copy方法:
1.3.1、浅拷贝:
SHDOG *dog = [[SHDog alloc]init];
__weak SHDog *dog1;
dog1 = dog;
</pre><pre name="code" class="objc">
一个指针释放另一个指针也会释放,节省空间
1.3.2、引用计数拷贝:
SHDOG *dog = [[SHDog alloc]init];
SHDog *dog1;
dog1 = dog;
一个指针释放,另一个指针不受影响,在OC中大量使用
1.3.3、深拷贝:
int *p1 = (int*)malloc(4);
*p1 = 10;
int *p2 = (int*)malloc(4);
*p2 = *p1;
两个指针各自有自己的堆空间
1.3.4、自定义类实现深拷贝时需要采纳NSCopying协议
//
// SHInteger.h
//
// Created by shuan on 16/8/24.
// Copyright © 2016年 shuan. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface SHInteger : NSObject<NSCopying>//采纳协议
@property int integer;
-(id)initWithInteger:(int)integer;
+(id)integerWithInteger:(int)integer;
-(void)show;
@end
1.3.5、在自定义类的.m文件中实现NSCopying协议中的方法copyWithZone
//
// SHInteger.m
//
// Created by shuan on 16/8/24.
// Copyright © 2016年 shuan. All rights reserved.
//
#import "SHInteger.h"
@implementation SHInteger
-(id)initWithInteger:(int)integer
{
if (self = [super init]) {
self.integer = integer;
}
return self;
}
+(id)integerWithInteger:(int)integer
{
__autoreleasing SHInteger *i = [[SHInteger alloc]initWithInteger:integer];
return i;
}
-(void)show
{
NSLog(@"%d",self.integer);
}
-(id)copyWithZone:(NSZone *)zone
{
SHInteger *i = [[SHInteger allocWithZone:zone]initWithInteger:self.integer];
return i;
}
@end
1.3.6、在主函数中用copy方法实现深拷贝,该方法在函数体中会自动调用copyWithZone
//
// main.m
//
// Created by shuan on 16/8/24.
// Copyright © 2016年 shuan. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "SHInteger.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
SHInteger *i1 = [SHInteger integerWithInteger:10];
// SHInteger *i2 = [SHInteger integerWithInteger:20];
// i2.integer = i1.integer;
SHInteger *i3 = [i1 copy];
[i3 show];
NSLog(@"%p",i1);
NSLog(@"%p",i3);
i3.integer = 20;
[i1 show];
}
return 0;
}
运行结果如下:
1.3.7、copy可以作为property的参数,是属性赋值时得到的是副本
@property(copy) SHBook *book;
1.4、类对象
1.4.1、类对象不是类的对象
1.4.2、是一种数据,用来在OC中唯一标识一个类
SHStudent *stu = [SHStudent studentWithName:@"张三" andAge:18];//stu是类的对象
[stu show];
Class c = [stu class];//变量c是一个类对象
Class c1 = [SHStudent class];//class方法既是实例方法又是类方法,实例方法与类方法可以重名
1.4.3、该种数据的数据类型是Class
1.4.4、该种数据调用class方法获取
具体如下:
1.4.5、类信息比较
1.4.5.1、isKindOfClass:判断某对象是否为指定类或其父类的对象
新建一个SHRectangle类再写一个SHSquare类继承SHRectangle,运行如下:
1.4.5.2、isMemberOfClass:判断某对象是否为指定类的对象(与父类无关,只判断指定类)
if ([s isMemberOfClass:[SHRectangle class]]) {
NSLog(@"对象s是类SHRectangle的对象");
}
1.4.5.3、isSubclassOfClass:判断某个类是否为指定类的子类
if ([s isMemberOfClass:[SHRectangle class]]) {
NSLog(@"对象s是类SHRectangle的对象");
}
1.5、方法选择器
1.5.1、也是一种新的数据,用于唯一标识类中的方法
1.5.2、该数据的数据类型是SEL
1.5.3、该数据可以通过@selector获得
SEL sel = @selector(study);
1.5.4、InstanceRespondToSelector:
判断一个类中是否有指定的方法
if ([SHStudent instancesRespondToSelector:sel]) {
NSLog(@"在SHStudent类中有study方法");
}
1.5.5、respondsToSelector:
判断一个对象是否能调用指定方法
if ([stu respondsToSelector:sel]) {
NSLog(@"对象stu可以调用study方法");
}
1.5.6、performSelector:
用于调用方法选择器标识的方法
[stu performSelector:sel];//[stu study]
if ([stu respondsToSelector:@selector(learn)]) {
[stu performSelector:@selector(learn)];
}
1.6、协议选择器
1.6.1、也是一种新的工具用于表示工程中的协议
1.6.2、该数据的数据类型是Protocol*
1.6.3、该数据可以通过@protocol获得
Protocol *p = @protocol(NSCopying);
1.6.4、conformsToProtocol:
判断一个类是否采纳了某一协议
if ([SHStudent conformsToProtocol:p]) {
NSLog(@"类SHStudent采纳了NSCopying协议");
}
if ([SHTeacher conformsToProtocol:p]) {
NSLog(@"类SHTeacher采纳了NSCopying协议");
}
思考练习:
1、写一个SHPoint,该类支持深拷贝
1.1、有两个属性x,y
1.2、有方法,带参初始化,带参工厂,show
2、写一个SHCircle类
2.1有一个圆心(支持深拷贝)和一个半径
2.2带参初始化,工厂,有求周长,面积
代码如下:
SHPoint.h:
#import <Foundation/Foundation.h>
@interface SHPoint : NSObject<NSCopying>
@property int x;
@property int y;
-(id)initWithX:(int)x andY:(int)y;
+(id)pointWithX:(int)x andY:(int)y;
-(void)show;
@end
SHPoint.m:
#import "SHPoint.h"
@implementation SHPoint
-(id)initWithX:(int)x andY:(int)y
{
if (self = [super init]) {
self.x = x;
self.y = y;
}
return self;
}
+(id)pointWithX:(int)x andY:(int)y
{
__autoreleasing SHPoint *p = [[SHPoint alloc]initWithX:x andY:y];
return p;
}
-(void)show
{
NSLog(@"(%d,%d)",self.x,self.y);
}
-(id)copyWithZone:(NSZone *)zone
{
SHPoint *p = [[SHPoint allocWithZone:zone]initWithX:self.x andY:self.y];
return p;
}
@end
SHCircle.h:
#import <Foundation/Foundation.h>
#import "SHPoint.h"
@interface SHCircle : NSObject
@property(copy) SHPoint *point;
@property int radius;
-(id)initWithPoint:(SHPoint*)point andRadius:(int)radius;
+(id)pointWithPoint:(SHPoint*)point andRadius:(int)radius;
-(void)show;
-(void)circumference;
-(void)area;
@end
SHCircle.m:
#import "SHCircle.h"
#define PI 3.1415
@implementation SHCircle
-(id)initWithPoint:(SHPoint *)point andRadius:(int)radius
{
if (self = [super init]) {
self.point = point;
self.radius = radius;
}
return self;
}
+(id)pointWithPoint:(SHPoint *)point andRadius:(int)radius
{
__autoreleasing SHCircle *c = [[SHCircle alloc]initWithPoint:point andRadius:radius];
return c;
}
-(void)circumference
{
NSLog(@"周长:%g",2*PI*self.radius);
}
-(void)area
{
NSLog(@"面积:%g",PI*self.radius*self.radius);
}
-(void)show
{
NSLog(@"%d (%d,%d)",self.radius,self.point.x,self.point.y);
}
@end
main.m:
#import <Foundation/Foundation.h>
#import "SHCircle.h"
#import "SHPoint.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
SHPoint *p = [SHPoint pointWithX:5 andY:6];
SHCircle *c = [SHCircle pointWithPoint:p andRadius:5];
if ([SHCircle instancesRespondToSelector:@selector(circumference)]) {
if ([c respondsToSelector:@selector(circumference)]) {
[c performSelector:@selector(circumference)];
}
}
if ([SHCircle instancesRespondToSelector:@selector(area)]) {
if ([c respondsToSelector:@selector(area)]) {
[c performSelector:@selector(area)];
[c show];
}
}
}
return 0;
}
今天就说到这
![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
原创允许转载,转载请以超链接形式指明出处