代码文件objective 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 "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 initSquareWithwidth:3 AndHeight:4 Andval:5];
        [squareObj print];
    }
    return 0;
}

文章结束给大家分享下程序员的一些笑话语录: 一个程序员对自己的未来很迷茫,于是去问上帝。
"万能的上帝呀,请你告诉我,我的未来会怎样?"
上帝说"我的孩子,你去问Lippman,他现在领导的程序员的队伍可能是地球上最大的"
于是他去问Lippman。
Lippman说"程序员的未来就是驾驭程序员"
这个程序员对这个未来不满意,于是他又去问上帝。
"万能的上帝呀,请你告诉我,我的未来会怎样?"
上帝说"我的孩子,你去问Gates,他现在所拥有的财产可能是地球上最多的"
于是他去问Gates。
Gates说"程序员的未来就是榨取程序员"
这个程序员对这个未来不满意,于是他又去问上帝。
"万能的上帝呀,请你告诉我,我的未来会怎样?"
上帝说"我的孩子,你去问侯捷,他写的计算机书的读者可能是地球上最多的"
于是他去问侯捷。
侯捷说"程序员的未来就是诱惑程序员"
这个程序员对这个未来不满意,于是他又去问上帝。
"万能的上帝呀,请你告诉我,我的未来会怎样?"
上帝摇摇头"唉,我的孩子,你还是别当程序员了")

你可能感兴趣的:(objective c)