//父类动物animal
//父类的声明
@interface animal : NSObject
{
int _age;
double _weight;
}
//动物吃set方法的声明
-(void)setAge:(int)age;
//动物吃get方法的声明
-(int)age;
//动物体重的set方法声明
-(void)setWeight:(double)weight;
//get方法
-(double)weight;
@end
@implementation animal
//动物吃get set方法的实现
-(void)setAge:(int)age
{
_age = age;
}
-(int)age
{
return _age;
}
//动物体重 set get方法的实现
-(void)setWeight:(double)weight
{
_weight = weight;
}
-(double)weight
{
return _weight;
}
@end
//Dog类
//Dog类声明
@interface Dog :animal
+(void)test;
@end
@implementation Dog
+(void)test
{
NSLog(@”调用了类的test方法”);
}
@end
//Cat类的声明
@interface Cat : animal
@end
@implementation Cat
@end
int main()
{
//创建对象 Dog
Dog *d = [Dog new];
//调用给的成员变量
[d setAge:10];
//创建对系那个Cat
Cat *c =[Cat new];
[c setWeight:3];
NSLog(@”Cat 的体重是%f”,[c weight]);
NSLog(@”Dog 年龄 %d”,[d age]);
return 0;
}