本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~
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 *)initSquareWith(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*)initSquareWith(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 "Rectangle.h"
#import "Square.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Rectangle *test=[[Rectangle alloc] init];
[test initWithWidth:3 AndHeight:4];
[test print];
Square *squareObj=[[Square alloc] init];
[squareObj initSquareWith3 AndHeight:4 Andval:5];
[squareObj print];
}
return 0;
}
文章结束给大家分享下程序员的一些笑话语录:
一个程序员对自己的未来很迷茫,于是去问上帝。
"万能的上帝呀,请你告诉我,我的未来会怎样?"
上帝说"我的孩子,你去问Lippman,他现在领导的程序员的队伍可能是地球上最大的"
于是他去问Lippman。
Lippman说"程序员的未来就是驾驭程序员"
这个程序员对这个未来不满意,于是他又去问上帝。
"万能的上帝呀,请你告诉我,我的未来会怎样?"
上帝说"我的孩子,你去问Gates,他现在所拥有的财产可能是地球上最多的"
于是他去问Gates。
Gates说"程序员的未来就是榨取程序员"
这个程序员对这个未来不满意,于是他又去问上帝。
"万能的上帝呀,请你告诉我,我的未来会怎样?"
上帝说"我的孩子,你去问侯捷,他写的计算机书的读者可能是地球上最多的"
于是他去问侯捷。
侯捷说"程序员的未来就是诱惑程序员"
这个程序员对这个未来不满意,于是他又去问上帝。
"万能的上帝呀,请你告诉我,我的未来会怎样?"
上帝摇摇头"唉,我的孩子,你还是别当程序员了")