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
//
// 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
//
// 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
#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];
2012-10-25 20:53:42.067 Circle[1307:707] Drawing a circle at (0 0 10 30) in Red
2012-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
这几天做一个自动扫描SD卡上所有APK文件的小工具,扫描过程中会把APK添加到LISTVIEW中显示,结果出现以下错误:(有时候触摸更新数据时候,触摸listview也会报错)
10-26 18:30:45.085: E/AndroidRuntime(7323): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131296280, class android.widget.ListView) with Adapter(class com.souapp.appmanager.ApkListAdapter)]
10-26 18:30:45.085: E/AndroidRuntime(7323): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131296280, class android.widget.ListView) with Adapter(class com.souapp.appmanager.ApkListAdapter)]
虽然自己很确定没有多线程操作,但是有人说listview本来就是线程不安全的,这个不关心了,看了国外一个开发者的方法很简单:
ListView.requestLayout(); Adatper.notifyDataSetChanged();
在你adpater更新前,调用listview的requestLayout(),这样做无非就是拟补数据数量不一致导致报错,虽然一个解决的好办法。
题外话:解决问题还是从本质上思考,从错误信息上提示,一定避免多线程去更新adapter的数据,为此我把多线程递归遍历SD卡目录修改成了单线程;
有的时候看到别人的一些补救方法未真正解决问题。
他抛下了自己一手打造的苹果,因病痛折磨离开了。对于这位巨人,我到底想说些什么呢?其实我也不太知道,有时候我是不太会说话的,只能说到哪就是哪了。