继承:
self:引用消息接受者
super:引用消息接受者的父类
@class 编译器不需要处理整个h文件,只要知道使用到的名字是个类名。
重写: 子类重新是现实父类中的某个方法,覆盖父类的方法.
1> 不允许子类和父类拥有相同名称的变量.
2> 父类必须声明在子类的前面
3> 调用某个对象的方法时,优先去当前的对象中找,如果找不到,去父类中找.
继承耦合性太高
图示:
@interface Persons : NSObject
{
int _age;
}
+(void)test;
-(void)test;
@end
@implementation Persons
-(void)test{
NSLog(@"Person - test");
}
+(void)test{
NSLog(@"Person + test");
}
@end
@interface Students : Persons
+(void)test2;
@end
@implementation Students
-(void)test{
NSLog(@"Student - test 重写父类方法");
}
+(void)test2{
[self test];//self表示当前类,也就是Student类 调用 +test方法,本类没有,会从父类方法中寻找
}
@end
继承和组合的区别:
继承: xxx 是 xxx 学生是人
组合: xxx 拥有 xxx 学生有分数
Super:
1> 直接调用父类中的方法
2> super处在对象方法中,就会调用父类的对象方法
super处在类方法中,就会调用父类的类方法
3> 使用场合:子类重写父类方法时,想保留父类的一些东西