我不知道接口(interface)如何解决我的问题,但我读到它可以通过接口(interface)here实现
问题:我创建了一个接口(interface),其中包含大约 3000 多个方法的所有声明,我在 3 个不同的类中实现这些方法,现在我想在主文件中调用 Interface 中的方法,原因是我需要任何类的任何方法,我不能扩展多个类,所以我考虑使用接口(interface)。
我可以这样做吗?非常感谢您的回答。
更新:使用扩展我可以使用 super.methodName();
这样我就不会创建对象。
我可以将这些方法拆分到不同的接口(interface)或不同的类中,但我必须在不创建对象的情况下访问这些方法,请 link了解我想做什么。
Update2: Interface ABC
//public int go()
函数在此声明
Class XYZ implements ABC
method go(object imp)
{.....}
另一类
Class PQR extends/implements ABC
{
// some code
int ret = super.go(this); OR int ret = obj.go(this)
} // What Should I use I now ABC is my interface but dont know where is it implemented so i want to call the go function how can I do this Please Explain what should i use.
谢谢
最佳答案
当您调用接口(interface)上的方法时,它实际上调用了具体类上的实现方法。不管你有多少个方法或者有多少个类,这并不重要。例如
List list = new ArrayList();
list.size(); // actually calls ArrayList.size()
顺便说一句:只有相对较少的类有 3000 行,更不用说 3000 个方法了。我认为这是生成的代码。
关于Java:如何从接口(interface)名称调用实现的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7553737/