您的位置:首页 > 其它

向类询问问题

2016-07-13 19:21 330 查看
一、

class-object 是一个类对象,selector是see类型的值

其中类对象例子:

例如一个Square类,则类对象[Square class]

mySquare是Square类的实例,获得他的类[mySquare class]

(1)-(BOOL) isKindOfClass: class-object

判断是否是这个类或者这个类的子类的实例

(2)-(BOOL) isMemberOfClass: class-object

判断是否是这个类的实例

(3)-(BOOL) respondsToSelector: selector

判读实例是否有这样方法

(4)+(BOOL) instancesRespondToSelector:selector

判断类是否有这个方法。此方法是类方法,不能用在类的对象

(5)+(BOOL)isSubclassOfClass: class-object

是否是一个类的子类

(6)-(id) performSelector: selector

Apply the method specified by selector.

(7)-(id) performSelector: selector withObject: object

Apply the method specified by selector pass- ing the argument object.

(8)-(id) performSelector: selector withObject: object1 withObject: object2

Apply the method specified by selector with the arguments object1 and object2.

运行时获取其类型的能力称为内省。内省可以有多种方法实现。

二、判断存储在变量中的对象是否来自同一个类

if([obj1 class]==[obj2 class])

三、例子

(1)

SEL sel = @selector (start:) ; // 指定action

if ([obj respondsToSelector:sel])

{ //判断该对象是否有相应的方法

[obj performSelector:sel withObject:self]; //调用选择器方法

}

(2)判断对象类型

Rectangle类

@interface Rectangle: NSObject

@property int width, height;

-(int) area;
-(int) perimeter;


@end

#import “Rectangle.h”

@implementation Rectangle

@synthesize width, height;

-(void) setWidth: (int) w andHeight: (int) h {

width = w;

height = h; }

-(int) area {

return width * height; }

-(int) perimeter {

return (width + height) * 2; }

@end

Square类

#import “Rectangle.h”

@interface Square: Rectangle

-(void) setSide: (int) s;

-(int) side;


@end



#import “Square.h”

@implementation Square: Rectangle

-(void) setSide: (int) s {

[self setWidth: s andHeight: s]; }

-(int) side

{


return self.width;

} @end

#import “Square.h”

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

@autoreleasepool {


Square *mySquare = [[Square alloc] init];

// isMemberOf: 判断是否是这个类的实例

if ( [mySquare isMemberOfClass: [Square class]] == YES )

NSLog (@”mySquare is a member of Square class”);

if ( [mySquare isMemberOfClass: [Rectangle class]] == YES )

NSLog (@”mySquare is a member of Rectangle class”);

if ( [mySquare isMemberOfClass: [NSObject class]] == YES )

// isKindOf: 判断是否是这个类或者这个类的子类的实例

if ( [mySquare isKindOfClass: [Square class]] == YES )

NSLog (@”mySquare is a kind of Square”);

if ( [mySquare isKindOfClass: [Rectangle class]] == YES )

NSLog (@”mySquare is a kind of Rectangle”);

if ( [mySquare isKindOfClass: [NSObject class]] == YES )

NSLog (@”mySquare is a kind of NSObject”);

// respondsTo: 判读实例是否有这样方法

if ( [mySquare respondsToSelector: @selector (setSide:)] == YES )

NSLog (@”mySquare responds to setSide: method”);

if ( [mySquare respondsToSelector: @selector (setWidth:andHeight:)] == YES )

NSLog (@”mySquare responds to setWidth:andHeight: method”);

if ( [Square respondsToSelector: @selector (alloc)] == YES )

NSLog (@”Square class responds to alloc method”);

// instancesRespondTo: 判断类是否有这个方法。此方法是类方法,不能用在类的对象

if ([Rectangle instancesRespondToSelector: @selector (setSide:)] == YES)

NSLog (@”Instances of Rectangle respond to setSide: method”);

if ([Square instancesRespondToSelector: @selector (setSide:)] == YES)

NSLog (@”Instances of Square respond to setSide: method”);

if ([Square isSubclassOfClass: [Rectangle class]] == YES)

NSLog (@”Square is a subclass of a rectangle”);

}

return 0; }

输出

mySquare is a member of Square class


mySquare is a kind of Square


mySquare is a kind of Rectangle


mySquare is a kind of NSObject


mySquare responds to setSide: method


mySquare responds to setWidth:andHeight: method

Square class responds to alloc method

Instances of Square respond to setSide: method

Square is a subclass of a rectangle
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  class 实例