Objective-C 笔记03

Objective-C

1、方法

//---- declare a multiparameter  method 
-(void) setAge:(int) age andName:(NSString *) name;
//define a multiparameter method
-(void) setAge:(int) age andName:(NSString *) name
{
   self.age=age;
   self.name=name;
}
//---- declare a method without parameter name
-(void) setAge:(int) age :(NSString *) name;
-(void) setAge:(int) age :(NSString *) name
{
  age=age;
  name=name;
}


局部变量
//局部变量y在定义时不会有初始值,所以使用前先赋值
//静态局部变量z有默认初始值0,不需要赋初始值;静态变量z可以用于统计方法被调用的次数。
-(int) calculate:(int) x
{ 
  int y=0;
  static int z;
  z+=1;
  y=2*x;
  return y;
} 

2、多态、动态绑定和id类型

  • 相同的名称,不同的类(不同的类共享相同方法名称的能力为多态)
  • id类型:可以用于存储任何类的对象,存储在变量中对象数据类型不是显示声明的。[id 类型声明时不需要*号]、[id类型不能用点运算符]

3、处理动态类型的方法

1 、 -(bool)isKindOfClass: 对象是不是某个类或其子类的实例

Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.


@interface AAA : NSObject
@property(strong,nonatomic) NSString *name; //声明属性
@property(nonatomic) NSInteger age;         //声明属性
-(void)setAge:(int) age andName:(NSString *) name;     //声明方法
-(void)print;
-(void)test;
@end
...

//举例:
@autoreleasepool {
 AAA *test=[AAA new];
 if([test isKindOfClass:[AAA class]])
    {
        NSLog(@"yes");
    }
    else
    {
        NSLog(@"no");
    }
}
   //output:yes
    

2 、 **-(bool)isMemberOfClass: 对象是不是某个类的成员 **

Returns a Boolean value that indicates whether the receiver is an instance of a given class.

  if([test isMemberOfClass:[AAA class]])
        {
            NSLog(@"yes"); //test 是类AAA的实例
        }
        else
        {
            NSLog(@"no");
        }
    

3 、 -(bool)respondsToSelector: 对象能否响应某个方法

Returns a Boolean value that indicates whether the receiving class responds to a given selector.

      if([test respondsToSelector:@selector(print)])
        {
            NSLog(@"yes it responds to  selector");
        }else
        {
            NSLog(@"not responds to selector");
        }

4 、 +(bool)instancesRespondToSelector: 类实例能否响应选择器

Returns a Boolean value that indicates whether instances of the receiver are capable of responding to a given selector.

     if([AAA instancesRespondToSelector:@selector(test)])
        {
            NSLog(@"yes it responds to  selector");
        }
        else
        {
               NSLog(@"not responds to selector");
        }
        

5、 +(bool)isSubclassOfClass: 对象是不是指定类的子类

Returns a Boolean value that indicates whether the receiving class is a subclass of, or identical to, a given class.

@interface BBB:AAA
@end
@implementation BBB
@end
     //BBB is subclass of class AAA
      if([BBB isSubclassOfClass:[AAA class]])
        {
            NSLog(@"BBB is subclass of AAA"); //yes
        }else{
            NSLog(@"BBB is not subclass of AAA");
        }
        // AAA is subclass of class AAA
         if([AAA isSubclassOfClass:[AAA class]])
        {
            NSLog(@"AAA is subclass of AAA"); //yes
        }else{
            NSLog(@"AAA is not subclass of AAA");
        }
        

6、 -(id)performSelector:selector 应用selector 指定的方法
-(id)performSelector:selector withObject: obj1
-(id)performSelector:selector withObject: obj1 withObject: obj2

Sends a message to the receiver with an object as the argument.

   [test performSelector:@selector(print)];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值