`
xuela_net
  • 浏览: 494747 次
文章分类
社区版块
存档分类
最新评论

ObjC学习3-类、继承、重载

 
阅读更多


1.继承

用一个矩形实例和一个正方形实例来学习继承,代码如下:


FRectangle继承于NSObject


#import <Foundation/Foundation.h>


@interface FRectangle : NSObject

{

int width;

int height;

}


@property int width,height;

-(int ) area;

-(int ) perimeter;



-(void) setW:(int) w AddH:(int) h;


@end



#import "FRectangle.h"


@implementation FRectangle

@synthesize width,height;


-(void) setW:(int)w AddH:(int)h

{

width = w;

height = h;

}


-(int) area{

returnwidth*height;

}


-(int) perimeter

{

return (width+height)*2;

}



@end



----FSquare继承于FRectangle


#import <Foundation/Foundation.h>

#import "FRectangle.h"

@interface FSquare : FRectangle



-(void) setSide:(int)s;

-(int) Side;


@end


#import "FSquare.h"


@implementation FSquare


-(void) setSide:(int)s

{

[self setW:s AddH:s];

}


-(int) Side

{

return width;

}

@end


主程序代码:

FRectangle *f = [[FRectanglealloc]init];

[f setW:1 AddH:2];

NSLog(@"w:%i h:%i",f.width,f.height);

NSLog(@"area is %i",[farea]);

NSLog(@"p is %i",[fperimeter]);

[f release];

NSLog(@"----");

FSquare *s = [[FSquarealloc]init];

[s setSide:4];

NSLog(@"square w is %i",s.Side);

NSLog(@"square area is %i, p is %i",s.area,s.perimeter);



结果:

2013-05-19 22:52:53.770 Test[704:c07] w:1 h:2

2013-05-19 22:52:53.771 Test[704:c07] area is 2

2013-05-19 22:52:53.773 Test[704:c07] p is 6

2013-05-19 22:52:53.774 Test[704:c07] ----

2013-05-19 22:52:53.775 Test[704:c07] square w is 4

2013-05-19 22:52:53.776 Test[704:c07] square area is 16, p is 16



2.class指令 - 内存分配

相当于import~但编译器在编译不需用去了解类名里面的方法,所以提高了效率

运用class指令改进Rectangle类

#import <Foundation/Foundation.h>


//#import "XYPoint.h"

@classXYPoint;

@interface XYRectangle :NSObject

{

int width;

int height;

XYPoint *origin;

}

@property int width,height;

//@property XYPoint origin;


-(XYPoint *) origin;

-(void) setOrigin :(XYPoint *) pt;


-(int ) area;

-(int ) perimeter;


-(void) setW:(int) w AddH:(int) h;


@end


... 省略


主要实现代码:

XYRectangle *myRect = [[XYRectanglealloc]init];

XYPoint *myPoint = [[XYPointalloc]init];

// [[myRect origin] setX:10 andY:20];

// NSLog(@"Origin at ( %i ,%i)",myRect.origin.x,myRect.origin.y);

[myPoint setX:100andY:200];

[myRect setW:10AddH:8];

myRect.origin = myPoint;

NSLog(@"Rectangle w = %i , h = %i",myRect.width,myRect.height);

NSLog(@"Origin at ( %i ,%i)",myRect.origin.x,myRect.origin.y);

[myPoint setX:20andY:10];

NSLog(@"Origin at ( %i ,%i)",myRect.origin.x,myRect.origin.y);

NSLog(@"area = %i ,perimeter = %i",[myRectarea],[myRectperimeter]);

[myRect release];

[myPoint release];


输出结果:

2013-05-20 00:48:18.836 Test[908:c07] Rectangle w = 10 , h = 8

2013-05-20 00:48:18.837 Test[908:c07] Origin at ( 100 ,200)

2013-05-20 00:48:18.838 Test[908:c07] Origin at ( 20 ,10)

2013-05-20 00:48:18.839 Test[908:c07] area = 80 ,perimeter = 36


* 两次Origin at 输出不一样,但我们并没有显式地调用myRect.origin去设置point ,这里说明了其实是传递了一个指针引用。

myRect的origin属性内存地址引用指向myPoint 所以myPoint改变时 myRect中的XYpoint原型也改变。我是这么理解的。。。


为了避免这个问题:

在setOrigin方法中做出一些改动,让他每次来到这个方法时重新分配下,但要记住把重新分配的对象在主程序中释放掉

-(void) setOrigin:(XYPoint *) pt

{

//origin = pt;//原先的

origin = [[XYPointalloc]init];

[origin setX:pt.x andY :pt.y];


}


{

...

[[myRect origin] release];//释放原点的内存是个负担= =~

[myRect release];

[myPoint release];

...

}

那么程序每次进来这里的时候都会重新分配一个自己的origin对象

接着重新执行程序,结果为:

2013-05-20 01:03:29.425 Test[955:c07] Rectangle w = 10 , h = 8

2013-05-20 01:03:29.426 Test[955:c07] Origin at ( 100 ,200)

2013-05-20 01:03:29.428 Test[955:c07] Origin at ( 100 ,200)

2013-05-20 01:03:29.429 Test[955:c07] area = 80 ,perimeter = 36



3.重载方法


#import <Foundation/Foundation.h>


@interface ClassA : NSObject


{

int x;

}


-(void) initVar;

-(void) printVar;

@end



#import "ClassA.h"


@implementation ClassA


-(void) initVar

{

x= 100;

}

-(void) printVar

{

NSLog(@"x=%i",x);

}

@end




---ClassB,继承于ClassA


#import "ClassA.h"


@interface ClassB : ClassA


-(void) initVar;

-(void) printVar;


@end


#import "ClassB.h"


@implementation ClassB

-(void) initVar

{

x =200;

}

-(void) printVar

{


NSLog(@"x=%i",x);

}

@end



主程序代码

ClassB *b = [[ClassBalloc]init];

ClassA *a = [[ClassAalloc]init];

[a initVar];

[a printVar];

[a release];

[b initVar];

[b printVar];

[b release];

结果:

2013-05-20 01:25:03.802 Test[1145:c07] x=100

2013-05-20 01:25:03.804 Test[1145:c07] x=200

两个方法发生重载,所以结果不一样。





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics