#import<Foundation/Foundation.h>spa
/*.net
1、繼承的好處:對象
(1)抽取重複代碼繼承
(2)創建了類之間的關係get
(3)子類能夠擁有父類中的全部成員變量和方法io
2、注意點class
(1)基本上全部類的根類是NSObjectimport
*/變量
/********Animal的聲明*******/方法
@interfaceAnimal : NSObject
{
int _age;
double _weight;
}
-(void)setAge:(int)age;
-(int)age;
-(void)setWeight:(double)weight;
-(double)weight;
/********Animal的實現*******/
@implementationAnimal
-(void)setAge:(int)age
{
_age = age;
}
-(int)age
{
return _age;
}
-(void)setWeight:(double)weight
{
_weight = weight;
}
-(double)weight
{
return _weight;
}
/********Dog*******/
// :Animal 繼承了Animal,至關於擁有了Animal裏面的全部成員變量和方法
//Animal稱爲Dog的父類
//Dog稱爲Animal的子類
@interface Dog : Animal
@end
@implementation Dog
@end
/********Cat*******/
@interfaceCat : Animal
@end
@implementationCat
@end
intmain()
{
Dog *d = [Dog new];
[d setAge:10];
NSLog(@"age=%d", [d age]);
return 0;
}
super的做用
1.直接調用父類中的某個方法
2.super處在對象方法中,那麼就會調用父類的對象方法
super處在類方法中,那麼就會調用父類的類方法
[super walk];//直接調用父類的walk方法
3.使用場合:子類重寫父類的方法時想保留父類的一些行爲