英文:
how to write a common method for two generated classes in different packages but all same methods in Java
问题
以下是翻译好的部分:
所以我有两个XSD生成的类。它们拥有共同的所有方法,并且两个类的所有方法都具有相同的参数。它们位于不同的包中。我应该如何编写一个通用方法,以便我可以一次为这两个类编写代码。
我试图以以下情形呈现:
package a.b.c;
class x{
void m(){};
int n(){};
string o(){};
}
另一个
package a.b.d;
class x{
void m(){};
int n(){};
string o(){};
}
英文:
So I have two XSD generated classes. They have all methods common and all methods of both classes have the same parameters as well. They are in different packages. How can I write a common method where I can do it once for both classes.
I am trying to present the scenario as below:-
package a.b.c;
class x{
void m(){};
int n(){};
string o(){};
}
Other one
package a.b.d;
class x{
void m(){};
int n(){};
string o(){};
}
答案1
得分: 1
Sure, here's the translation:
如果这两个类共享一个方法,那听起来非常像它们需要有一个共同的祖先类,该祖先类具有那个方法,并且通过你的两个类都来扩展那个基类。当然,你可以扩展来自不同包的类:
public class ChildClass extends com.the.other.package.BaseClass {
public void foo() {
//...
}
}
英文:
If the two classes share a method, that sounds very
much like they need to have a common ancestor class, having that method and extend
that base class by both your classes. You can of course extend
classes of different packages:
public class ChildClass extends com.the.other.package.BaseClass {
public void foo() {
//...
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论