简体   繁体   English

使同一类的不同版本通用的方法

[英]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.Foomodel.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.

相关问题 Java 8方法签名相同方法的不同版本 - Java 8 Method Signature different versions of same method 如何在同一应用程序中使用不同版本的 class? - How to use different versions of a class in the same application? Java - 如何加载同一类的不同版本? - Java - how to load different versions of the same class? Java:对两个不同版本的依赖类使用相同的代码 - Java: Use the same code with two different versions of a dependent class 同一Java类的两个版本,每个版本具有不同的超类 - Two versions of the same Java class where each has a different superclass 使用更改的构造函数和“抽象”修饰符实例化相同 class 的不同版本 - Instantiate different versions of same class with changed constructor and "abstract" modifier 如何为不同的SDK目标管理同一类文件的多个版本? - How to manage multiple versions of same class file for different SDK targets? 同一库的不同版本 - Different versions of the same library JAXB和类的不同版本 - JAXB and different versions of the class 我怎样才能有单独的 gradle 项目的调试和发布版本,它们可以引入相同 class 的不同版本? - How can I have separate debug and release versions of gradle project that pull in different versions of the same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM