开发者

Problem with providing the methods

I'm using gwt framework called smartgwt (however, problem concerns gwt and java) There, you can find HLayout class which can contain members. You can add them using:

addMember(Canvas component)

I created RectConainer class which extends HLayout class. Then, I created another class Rect which extends Canvas class indirectly. Right now, I want RectConainer to provide:

addMember(Rect component)

instead of:

addMember(Canvas component)

In other words, I want RectConainer to provide all inherited method + addMember(Rect component), but without addMember(Canvas component). The only way how to do it (which I know) is to use Composite class, but then I block all inherited m开发者_如何学JAVAethods. Because I've many of them then, I'd have to write many line of code to provide them again. So do you have any better ideas how to solve this problem?

Thanks in advance


If I understand you correctly, you have public addMember(Canvas component) method in HLayout class and you want to "hide" it in your RectConainer (Providing addMember(Rect component) instead). The short answer is "it is not possible to hide addMember(Canvas component)", because it would brake polymorphism, i.e. your child class (RectConainer ) would not be able to provide the contract (interface) of your parent class (HLayout).

What you can do (I cannot think of any good reason to do it, but still) is this:

@Override
public void addMember(Canvas component) {
    throw new UnsupportedOperationException("Do not call this method. Call addMember(Rect component) instead");
}

But again, think twice before doing this: this method is a part of some contract that your framework might count on. If you want to limit Canvas that you can pass to your addMember method, simply do type check:

@Override
public void addMember(Canvas component) {
    if (!Rect.class.isInstance(component)) {
        throw new IlligalArgumentException("Only Rect is accepted. Sorry...");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜