force base class使用自己的方法而不是覆盖方法 [英] force base class to use its own method and not overrided method

查看:102
本文介绍了force base class使用自己的方法而不是覆盖方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的场景:

class SomeBaseClass
{
     public void foo(String str)
     {
     .......
     }
     public void foo(String strs[])
     {
     .......
     }
}

class MyClass extends SomeBaseClass
{
     public void foo(String str)
     {
           super.foo(str);
     }
     public void foo(String strs[])
     {
           throw new RuntimeException("only one input please!");
     }
}

逻辑非常简单。 SomeBaseClass是我无法修改的第三方工具。我想限制它的功能并且不想允许foo(String strs [])。

The logic is pretty simple. "SomeBaseClass" is 3rd party tool that i cannot modify. I want to limit its functionality and don't want to allow foo(String strs[]).

问题是在SomeBaseClass里面foo(Sting str)内部调用foo (String strs [])。因此,当我调用MyClass的foo(String str)时,我得到一个运行时异常。如何告诉SomeBaseClassclass使用SomeBaseClass :: foo(String strs [])和所有其他类使用MyClass :: foo(String strs [])

the problem is that inside SomeBaseClass foo(Sting str) internally calls foo(String strs[]). Hence when i call foo(String str) of MyClass, I get a runtime exception. How can I tell the SomeBaseClassclass to use SomeBaseClass::foo(String strs[]) and all other classes to use MyClass ::foo(String strs[])

推荐答案

也许是在engg但是在 MyClass.foo(String strs [])里面你可以检查调用者是否是 SomeBaseClass .foo(String str),如果是的话,让调用通过 super.foo(String) else throw RuntimeException

Perhaps over engg but inside MyClass.foo(String strs[]) you can check if the caller is SomeBaseClass.foo(String str), if yes, let the call go thru to super.foo(String) else throw RuntimeException.

要查找调用者,请检查 StackTrace

To find the caller check StackTrace.

这篇关于force base class使用自己的方法而不是覆盖方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆