我真的他妈的有点苦笑不得。
wuliaotd:小弟,不是我说你,你真的有点无聊透顶。
别说你j2ee很xx。
别和我说 机2ee。要说,好啊,
给你个题目,罗列出j2ee所涵盖的技术来,让大家开开眼界。你还可以针对这些技术,
适当做些点评其优劣。有理,大家自然会说最讨厌的“I服了U”。
你还有一个毛病,自欺欺人。
另外一个建议,不要以为全世界就你一个人。
介于你针对我说的一些话,我告诉你一些事实。我从98年开始学习java,只是学习。
我在2001年开始j2ee。不算早,也不算迟。此外,我知道我自己需要学习什么,
我也知道我能帮助别人些什么。我也知道一点中国的程序员和开发者要什么。
但是我确实不知道你要什么。
下面就是一些c代码,你可以看看。但确实不是我写的。也不能证明我会写或者不会写。
证明什么呢?证明我说的别人在写。
In C, we have to manage the vtable explicitly, and we also have to inherit from the base class vtable, so that virtual methods that aren't overridden are available to the derived class. This forces two separate inheritances: data and vtable. The public interface is the same, except for the addition of prototypes for Shape_area() and Shape_draw(), so I won't repeat it. The only changes to the private code are the new and delete methods, and the added methods:
#define VTABLE(this) ((void***)((char*)(this)-sizeof(void*)))
static void* Shape_vtable[] = {
0, 0 /* both of our virtual methods are pure virtuals */
};
Shape* Shape_new(Shape* this, int _x, int _y) {
if(!this)
this = malloc(sizeof(void*) + /* vtable pointer */
sizeof(Shape) + sizeof(Shape_private));
Shape* obj = (Shape*)((char*)this+sizeof(void*));
/* sneaky vtable hiding */
*VTABLE(obj) = Shape_vtable;
Shape_private* priv = SHAPE_PRIVATE(obj);
obj->x = _x;
obj->y = _y;
return obj;
}
void Shape_delete(Shape* this) {
free(VTABLE(this));
}
#define CIRCLE(this) ((Circle*)(SHAPE_PRIVATE((Shape*)this)+1))
double Circle_area(Circle* this) {
Circle* derived = CIRCLE(this);
Circle_private* priv = CIRCLE_PRIVATE(derived);
return M_PI*priv->radius*priv->radius;
}
double Circle_draw(Circle* this, Canvas* c) {
Circle* derived = CIRCLE(this);
Circle_private* priv = CIRCLE_PRIVATE(derived);
/* ... */
}
static void* [] Circle_vtable = {
Circle_area, Circle_draw
};
Circle* Circle_new(Circle* this, int _x, int _y, int _radius) {
if(!this)
this = malloc(sizeof(void*) +
sizeof(Shape) + sizeof(Shape_private) +
sizeof(Circle) + sizeof(Circle_private));
this = (Circle*)Shape_new((Shape*)this,_x,_y);
*VTABLE(this) = Circle_vtable;
Circle* derived = CIRCLE(this);
Circle_private* priv = CIRCLE_PRIVATE(derived);
priv->radius = _radius;
return this;
}
void Circle_delete(Circle* this) {
return Shape_delete((Shape*)this);
}
#define FILLEDCIRCLE(this) ((FilledCircle*)(CIRCLE_PRIVATE((Circle*)CIRCLE(this))+1))
void FilledCircle_draw(FilledCircle* this, Canvas* c) {
FilledCircle* derived = FILLEDCIRCLE(this);
/* ... */
}
void* FilledCircle_vtable[] = {
Circle_area, FilledCircle_draw
};
FilledCircle* FilledCircle_new(FilledCircle* this, int _x, int _y, int _radius) {
if(!this)
this = malloc(sizeof(void*) +
sizeof(Shape) + sizeof(Shape_private) +
sizeof(Circle) + sizeof(Circle_private) +
sizeof(FilledCircle) + sizeof(FilledCircle_private));
this = (FilledCircle*)Circle_new((Circle*)this,_x,_y,_radius);
*VTABLE(this) = FilledCircle_vtable;
FilledCircle* derived = FILLEDCIRCLE(this);
FilledCircle_private* priv = FILLEDCIRCLE_PRIVATE(derived);
/* would initialize new members here */
return this;
}
void FilledCircle_delete(FilledCircle* this) {
return Circle_delete((Circle*)this);
}
And finally, how to make use of our polymorphic objects: C++:
Shape* rc = new RedCircle(10,10,8);
cout << "Our circle has area " << rc->area() << endl;
rc->draw(canvas);
// draw it
delete rc;
And our C emulation (the VTABLE macro has to be made public, or perhaps an inline function could be used to wrap its use):
Shape* rc = (Shape*)RedCircle_new(NULL,10,10,8);
printf("Our circle has area %d/n", (*VTABLE(rc))[0]->(rc));
(*VTABLE(rc))[1]->(rc,canvas);
/* draw it */
Shape_