Objectvie-C继承、新增类、内部函数调用、外部函数调用
2012-10-26 16:07
405 查看
1. 四个类,一个main.m
2. 代码实例:
main.m//
// main.m
// Circle
//
// Created by macsir on 12-10-25.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import<Foundation/Foundation.h>
#import"Circle.h"
#import"Rectangle.h"
#import"PblateSphereiod.h"
#import"Triangle.h"
NSString *colorName (ShapeColor colorName);
void drawShapes (id shapes[],int count);
void drawTriangle(ShapeRect bounds,ShapeColor fillColor);
void drawEgg(ShapeRect bounds,ShapeColor fillColor) ;
void drawRectangle(ShapeRect bounds,ShapeColor fillColor) ;
void drawCircle(ShapeRect bounds,ShapeColor fillColor) ;
int main (int argc,constchar * argv[])
{
// @autoreleasepool {
//
// // insert code here...
// NSLog(@"Hello, World!");
//
// }
// Circle circle;
// circle.draw();
id shapes[4];
ShapeRect rect0 = {0,
0, 10, 30};
shapes[0] = [Circlenew];
[shapes[0]setBounds:rect0];
[shapes[0]setFillColor:kRedColor];
ShapeRect rect1 = {30,
40, 50, 60};
shapes[1] = [Rectanglenew];
[shapes[1]setBounds:rect1];
[shapes[1]setFillColor:kGreenColor];
ShapeRect rect2 = {0,
0, 10, 30};
shapes[2] = [PblateSphereiodnew];
[shapes[2]setBounds:rect2];
[shapes[2]setFillColor:kBlueColor];
ShapeRect rect3 = {47,
32, 80, 50};
shapes[3] = [Trianglenew];
[shapes[3]setBounds:rect3];
[shapes[3]setFillColor:kRedColor];
drawShapes(shapes, 4);
return 0;
}
void drawShapes (id shapes[],int count)
{
int i;
for(i = 0; i<count; i++) {
[shapes[i]
draw];
}
}//drawShapes
3. 基础类:
//// Circle.h
// Circle
//
// Created by macsir on 12-10-25.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import<Foundation/Foundation.h>
#import"PublicData.h"
@interface Circle :NSObject
{
ShapeColor fillColor;
ShapeRect bounds;
}
- (NSString*) getColorName: (ShapeColor) colorName;
- (void) setFillColor: (ShapeColor) fillColor;
- (void) setBounds: (ShapeRect) bounds;
- (void) draw;
@end//Circle
//
// Circle.m
// Circle
//
// Created by macsir on 12-10-25.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import"Circle.h"
@implementation Circle//用于定义类的公共接口
- (NSString *) getColorName: (ShapeColor)c
{
switch (c) {
case kRedColor:
return @"Red";
break;
casekGreenColor:
return @"Green";
break;
case kBlueColor:
return @"Blue";
break;
default:
break;
}
return@"no clue";
}//myColorName
- (void) setFillColor: (ShapeColor) c
{
fillColor = c;
}//setFillColor
- (void) setBounds: (ShapeRect) b
{
bounds = b;
}//setBounds
- (void) draw
{
NSLog(@"Drawing a circle at (%d %d %d %d) in
%@",bounds.x ,bounds.y,bounds.width,bounds.height,
[selfgetColorName:fillColor]);
}
@end
4. 继承类:
//// Rectangle.h
// Circle
//
// Created by macsir on 12-10-25.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import<Foundation/Foundation.h>
#import"PublicData.h"
#include"Circle.h"
@interface Rectangle :Circle
@end
//
// Rectangle.m
// Circle
//
// Created by macsir on 12-10-25.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import"Rectangle.h"
@implementation Rectangle
@end
其他继承类大同。
5. 总结:
*继承:
#include "Circle.h"@interface Rectangle :Circle
*内部接口调用:
NSLog(@"Drawing a circle at (%d %d %d %d) in %@", bounds.x , bounds.y, bounds.width, bounds.height,[selfgetColorName:fillColor]);
*外部类的接口调用:
#import"Circle.h"
id shapes[4];ShapeRect rect0 = {0,
0, 10, 30};
shapes[0] = [Circlenew];
[shapes[0]setBounds:rect0];
[shapes[0]setFillColor:kRedColor];
6. 运行结果
2012-10-25 20:53:42.067 Circle[1307:707] Drawing a circle at (0 0 10 30) in Red2012-10-25 20:53:42.071 Circle[1307:707] Drawing a circle at (30 40 50 60) in Green
2012-10-25 20:53:42.073 Circle[1307:707] Drawing a circle at (0 0 10 30) in Blue
2012-10-25 20:53:42.075 Circle[1307:707] Drawing a circle at (47 32 80 50) in Red
相关文章推荐
- js函数内部调用的含有回掉的函数,其中回掉函数可以拿到外部函数传递的实参
- C语言-内部函数与外部函数+调用实例
- js从$scope外部调用$scope内部函数,跨js调用非全局函数
- function ain=innerfun() ain=2*b;内部函数调用外部变量
- JavaScript—在嵌套的内部函数中调用外部this的方法
- 透过汇编另眼看世界之多继承下的虚函数函数调用(二)
- 关于virtual、非virtual继承函数的调用
- 嗯,关于JS函数内部变量的赋值和调用以及this的用法
- jar包调用jar包内部和外部的配置文件
- C的内部函数和外部函数
- 通过虚函数继承,父类调用子类中函数
- 简单讲解C++的内部和外部函数以及宏的定义
- go程序中可以被外部包调用的函数和变量
- 调用内部类的私有方法,其中内部类和外部类的构造器都是私有的
- JavaScript学习(一)——调用内部js和外部js的例子
- C++直接调用外部函数
- javaScript 自执行函数—保留异步方法调用的内部变量
- 【转】doxygen+graphviz生成工程中的类继承树及函数调用图
- c++中全局变量、局部变量、内部函数、外部函数
- 父类的正常成员函数子类可以使用,并不是继承下来 的,子类是怎么调用父类的函数 : 可能是利用了一种叫函数名联编的方法.没有隐藏的情况下用函数名字来决定调用的函数.