您的位置:首页 > 其它

oc-18-继承

2016-03-23 10:17 246 查看
//Animal.h
#import <Foundation/Foundation.h>

@interface Animal : NSObject
{
int _age; // 不写@public,默认只能在本类和子类当中访问.间接继承.
}
@end

//Animal.m
#import "Animal.h"
@implementation Animal
{
int _weight; // 写在.m文件中的成员变量,叫做私有成员变量.只能在本类当中访问,子类不能访问或者设置
}
@end

//Dog.h
#import "Animal.h"

@interface Dog : Animal
- (void)lookHome;
@end
//Dog.m
#import "Dog.h"

@implementation Dog
- (void)lookHome
{
_age = 1;
//    _weight = 9; // 私有变量,子类不能访问或者设置
NSLog(@"看家......");
}
@end


.h文件中子类父类不能定义同名的变量,.m文件中子类父类可以有相同的变量名。

super调用的直接父类,直接父类没有就是爷爷类。super在对象方法中只能调用父类的对象方法,在类方法中只能调用父类的类方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: