向类询问问题

一、
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员的修养

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值