how to write a common method for two generated classes in different packages but all same methods in Java

huangapple go评论73阅读模式
英文:

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() {
        //...
    }
}

huangapple
  • 本文由 发表于 2020年9月19日 16:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63966593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定