第一、objective C中继承的实现
在oc中只有实例变量会有权限控制,实例方法和类方法是没有权限控制的,这点与c++不同,OC默认的是protected,并且在声明权限控制时,没有分号
在OC中可以像C++一样用指针运算法来访问实例变量
Rectangle.h 文件代码:
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
{
int _width;
int _height;
}
@property (nonatomic,assign) int width;
@property (nonatomic,assign) int height;
-(Rectangle *) initWithWidth:(int)w AndHeight:(int)h;
-(void) print;
@end
Rectangle.m 文件代码:
#import "Rectangle.h"
@implementation Rectangle
@synthesize width=_width;
@synthesize height=_height;
-(Rectangle *) initWithWidth:(int)w AndHeight:(int)h
{
self=[super init];
if(self)
{
self.width=w;
self.height=h;
}
return self;
}
-(void)print
{
NSLog(@"the height is %d ,the width is %d",self.height,self.width);
}
@end
Square.h 文件代码:
#import "Rectangle.h"
@interface Square : Rectangle
{
int _d;
}
-(Square *)initSquareWithwidth:(int)w AndHeight:(int)h Andval:(int)d;
@property (nonatomic,assign)int d;
@end
Square.m 文件代码:
#import "Square.h"
@implementation Square
@synthesize d=_d;
-(Square*)initSquareWithwidth:(int)w AndHeight:(int)h Andval:(int)d
{
self=[super init];
if(self)
{
self.height=h;
self.width=w;
self.d=d;
}
return self;
}
-(void)print //实现重载
{
NSLog(@"the d is %d,the width is %d,the height is %d",self.d,self.width,self.height);
}
@
main函数:
#import <Foundation/Foundation.h>
#import "Access.h"
#import "Re
在oc中只有实例变量会有权限控制,实例方法和类方法是没有权限控制的,这点与c++不同,OC默认的是protected,并且在声明权限控制时,没有分号
在OC中可以像C++一样用指针运算法来访问实例变量
Rectangle.h 文件代码:
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
{
int _width;
int _height;
}
@property (nonatomic,assign) int width;
@property (nonatomic,assign) int height;
-(Rectangle *) initWithWidth:(int)w AndHeight:(int)h;
-(void) print;
@end
Rectangle.m 文件代码:
#import "Rectangle.h"
@implementation Rectangle
@synthesize width=_width;
@synthesize height=_height;
-(Rectangle *) initWithWidth:(int)w AndHeight:(int)h
{
self=[super init];
if(self)
{
self.width=w;
self.height=h;
}
return self;
}
-(void)print
{
NSLog(@"the height is %d ,the width is %d",self.height,self.width);
}
@end
Square.h 文件代码:
#import "Rectangle.h"
@interface Square : Rectangle
{
int _d;
}
-(Square *)initSquareWithwidth:(int)w AndHeight:(int)h Andval:(int)d;
@property (nonatomic,assign)int d;
@end
Square.m 文件代码:
#import "Square.h"
@implementation Square
@synthesize d=_d;
-(Square*)initSquareWithwidth:(int)w AndHeight:(int)h Andval:(int)d
{
self=[super init];
if(self)
{
self.height=h;
self.width=w;
self.d=d;
}
return self;
}
-(void)print //实现重载
{
NSLog(@"the d is %d,the width is %d,the height is %d",self.d,self.width,self.height);
}
@
main函数:
#import <Foundation/Foundation.h>
#import "Access.h"
#import "Re