OC 基础04
用@property int age; // 就可以代替下面的两行
- (int)age; // getter
2.在@property后面写上需要生成getter/setter方法声明的属性名称, 注意因为getter/setter方法名称中得属性不需要_,
所以@property后的属性也不需要_.并且@property和属性名称之间要用空格隔开
{
int _age;
}
@property int age;
@end
@interface Person : NSObject
{
@public
int _age;
int age;
}
@property int age;
@end
int main(int argc, const char * argv[]) {
Person *p = [Person new];
[p setAge:30];
NSLog(@"age = %i, _age = %i", p->age, p->_age);
return 0;
}
Person *p = [Person new];
[p setAge:-10];
NSLog(@"age = %i", [p age]);
@property (readonly) int age;
@property (getter=isMarried) BOOL married;
说明,通常BOOL类型的属性的getter方法要以is开头
self = [super init];
if (self) {
// Initialize self.
}
return self;
[super init]的作用:
self 为什么要赋值为[super init]:
if (self = [super init]) {
// Initialize self.
}
return self;
self = [super init]
写成self ==
[super init]
// init此时返回值是id
NSString *str = [[Person alloc] init];
// Person并没有length方法, 但是id是动态类型, 所以编译时不会报错
NSLog(@"length = %i", str.length);
// init此时返回值是instancetype
// 由于instancetype它会进行类型检查, 所以会报警告
NSString *str = [[Person alloc] init];
NSLog(@"length = %i", str.length);
instancetype *p = [[person alloc] init];
@property NSString *name;
// 当想让对象一创建就拥有一些指定的值,就可以使用自定义构造方法
@end
@property int age;
@end
@interface Student : Person
@property NSString *name;
@end
@implementation Student
{
if (self = [super init]) {
// 这个_Age是父类中通过property自动在.m中生成的无法继承,不能直接访问
// _age = age;
[self setAge:age];
_name = name;
}
return self;
}
@interface Person : NSObject
@property int age;
@end
@interface Student : Person
@property NSString *name;
@end
@implementation Student
{
if (self = [super initWithAge:age]) {
_name = name;
}
return self;
}
+ (id)person
{
return [[Person alloc]init];
}
+ (id)personWithAge:(int)age;
+ (id)personWithAge:(int)age
{
Person *p = [[self alloc] init];
[p setAge:age];
return p;
[NSSet setWithObject:<#(id)#>];
@interface Person : NSObject
+ (id)person;
@end
@implementation Person
+ (id)person
{
// 谁调用这个方法,self就代表谁
// 注意:以后写类方法创建初始化对象,写self不要直接写类名
return [[self alloc]init];
}
@end
@interface Student : Person
@end
@implementation Student
@end
int main(int argc, const char * argv[])
{
}
如: [obj class];
如:[Person class]
Person
test];
Person
class];
[c test];
Person
*g = [
Person
new];
Person
class];
Person
*g1 = [c new];
Dog *dog=[[Dog alloc] init];
[dog eat];
BOOL flag;
// [类 respondsToSelector]用于判断是否包含某个类方法
flag = [Person respondsToSelector:@selector(objectFun)]; //NO
flag = [Person respondsToSelector:@selector(classFun)]; //YES
Person *obj = [[Person alloc] init];
// [对象 respondsToSelector]用于判断是否包含某个对象方法
flag = [obj respondsToSelector:@selector(objectFun)]; //YES
flag = [obj respondsToSelector:@selector(classFun)]; //NO
// [类名 instancesRespondToSelector]用于判断是否包含某个对象方法
// instancesRespondToSelectorr只能写在类名后面, 等价于 [对象 respondsToSelector]
flag = [Person instancesRespondToSelector:@selector(objectFun)]; //YES
flag = [Person instancesRespondToSelector:@selector(classFun)]; //NO
Person *p = [Person new];
SEL s1 = @selector(objectFun);
[p performSelector:s1];
SEL s3 = @selector(objectFun:value2:);
SEL s4 = @selector(classFun);
[Person performSelector:s4];
SEL s5 = @selector(classFun:);
SEL s6 = @selector(classFun:value2:);
@implementation Person
- (void)makeObject:(id) obj performSelector:(SEL) selector
{
[obj performSelector:selector];
}
@end
int main(int argc, const char * argv[]) {
Person *p = [Person new];
SEL s1 = @selector(eat);
Dog *d = [Dog new];
[p makeObject:d performSelector:s1];
return 0;
}