代码中用到的类
Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
+(void)speak;
@end
Person.m
#import "Person.h"
@implementation Person
+(void)speak{
NSLog(@"I'm a person!");
}
@end
Student.h
#import "Person.h"
@interface Student : Person{
int age;
}
-(Student *)initAge:(int)age;
-(void)setAge:(int)age;
-(void)setAge1:(NSString *)age;
-(int)age;
@end
Student.m
#import "Student.h"
@implementation Student
-(Student *)initAge:(int)age{
self = [super init];
if(self){
self->age = age;
}
return self;
}
-(void)setAge:(int)age{
self->age = age;
}
//这里是为了反射调用时作准备
-(void)setAge1:(NSString *)age{
self->age = [age intValue];
}
-(int)age{
return age;
}
@end
1、类的反射【实例化对象】
Class name = NSClassFromString(@"Student");
Student *stu = [[name alloc] init];
NSLog(@"%@",stu);