[英]Making a Method common for different versions of same class
I am generating Classes from XSD. 我正在从XSD生成类。 For each revision in XSD, A new class is generated with same name but under a different package.
对于XSD中的每个修订版,将使用相同的名称但使用不同的包来生成一个新类。 If "Foo"is the class, the class name will be : model.v1.Foo , model.v2.Foo etc. fro each revision.
如果“ Foo”是类,则每个版本的类名称将是: model.v1.Foo , model.v2.Foo等。
Irrespective of the version of class, some operations are common. 与类的版本无关,某些操作是常见的。 Is there any way to create a common method, that can take up any version of Foo class
有什么方法可以创建可以占用Foo类任何版本的通用方法
public static String doOperation(Foo foo){
//Do some operation
}
The DoOperation method needs to be common for all the versions. DoOperation方法对于所有版本都必须是通用的。
One possibility is create an interface like: 一种可能性是创建一个界面,如:
public interface FooIF {
public default String doOperation(Foo foo){
System.out.println("doOperation executed");
return null;
}
}
And have Foo
implement it: 并让
Foo
实现它:
public class Foo implements FooIF{
public Foo() {
}
public static void main( String[] args) {
Foo f = new Foo();
f.doOperation(null);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.