定义一个狗类- #import <Foundation/Foundation.h>
- @interface Dog : NSObject
- -(void)test;
- +(void)test;
- @end
复制代码
实现狗类- #import "Dog.h"
- @implementation Dog
- -(void) test
- {
- NSLog(@"对象方法test");
- }
- +(void)test
- {
- NSLog(@"类方法test");
- }
- @end
复制代码
实现类对象的获取
- #import <Foundation/Foundation.h>
- #import "Dog.h"
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- // insert code here...
- Dog *dog= [Dog new];
- [dog test];//对象方法
- [Dog test];//类方法
-
- Dog *d= [Dog new];
- //类对象的获取方法
- //类对象属于Class
- //1、通过实例对象获取
-
- Class c1 = [dog class];
- Class c2 = [d claaa];
-
- NSLog(@"%p",c1);
- NSLog(@"%p",c2);
-
- //2、通过类名类获取对象
- Class c3 =[Dog class];
- NSLog(@"%p",c3);
- }
- return 0;
- }
复制代码 |
|