黑马程序员技术交流社区

标题: 类的本质之类对象的获取 [打印本页]

作者: mazhichao    时间: 2015-7-3 19:28
标题: 类的本质之类对象的获取
定义一个狗类
  1. #import <Foundation/Foundation.h>

  2. @interface Dog : NSObject
  3. -(void)test;
  4. +(void)test;
  5. @end
复制代码

实现狗类
  1. #import "Dog.h"

  2. @implementation Dog
  3. -(void) test
  4. {
  5.     NSLog(@"对象方法test");
  6. }
  7. +(void)test
  8. {
  9.     NSLog(@"类方法test");
  10. }
  11. @end
复制代码

实现类对象的获取
  1. #import <Foundation/Foundation.h>
  2. #import "Dog.h"
  3. int main(int argc, const char * argv[]) {
  4.     @autoreleasepool {
  5.         // insert code here...
  6.         Dog *dog= [Dog new];
  7.         [dog test];//对象方法
  8.         [Dog test];//类方法
  9.         
  10.         Dog *d= [Dog new];
  11.         //类对象的获取方法
  12.         //类对象属于Class
  13.         //1、通过实例对象获取
  14.         
  15.         Class c1 = [dog class];
  16.         Class c2 = [d claaa];
  17.         
  18.         NSLog(@"%p",c1);
  19.         NSLog(@"%p",c2);
  20.         
  21.         //2、通过类名类获取对象
  22.         Class c3 =[Dog class];
  23.         NSLog(@"%p",c3);
  24.     }
  25.     return 0;
  26. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2