1:默认的实例变量的作用域范围是:这个类体内.默认继承的实例变量可以直接使用。 2:实例变量的权限控制符号 3:默认是projected 4:oc中只支持单继承 5:共性的东西放在父类,个性的东西放在子类
下面使用汽车的继承关系来演示一下默认的权限(protected)
一:汽车Car.h
// // Car.h // Car // // Created by hmjiangqq on 14-1-22. // Copyright (c) 2014年 hmjiangqq. All rights reserved. // #import <Foundation/Foundation.h> @interface Car : NSObject { @protected //默认权限 int cID; //id float speed; //车速 NSString *name; //名称 } -(void)run; @end 二:汽车Car.m // // Car.m // Car // // Created by hmjiangqq on 14-1-22. // Copyright (c) 2014年 hmjiangqq. All rights reserved. // #import "Car.h" @implementation Car //实例化汽车的属性 -(id)init{ if(self=[super init]){ cID=1000; speed=120; name=@"小汽车"; } return self; } -(void)run{ NSLog(@"start ..."); } @end三:BMW.h // // BMW.h // Car // // Created by hmjiangqq on 14-1-22. // Copyright (c) 2014年 hmjiangqq. All rights reserved. // #import "Car.h" @interface BMW : Car @end四:BMW.m // // BMW.m // Car // // Created by hmjiangqq on 14-1-22. // Copyright (c) 2014年 hmjiangqq. All rights reserved. // #import "BMW.h" @implementation BMW -(void)run{ name=@"宝马"; NSLog(@"car name is %@\n",name); } @endmain.m // // main.m // Car // // Created by hmjiangqq on 14-1-22. // Copyright (c) 2014年 hmjiangqq. All rights reserved. // #import <Foundation/Foundation.h> #import "Car.h" #import "BMW.h" int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); Car *car=[[Car alloc]init]; [car run]; BMW *bmw=[[BMW alloc]init]; [bmw run]; } return 0; } |