简体   繁体   English

如何从非静态接口方法实现 static 方法?

[英]How to achieve static method from non-static interface method?

I have an interface like this:我有一个这样的界面:

public interface Byteable<T> {
    byte[] toBytes();
    T fromBytes(byte[] bytes);
}

which, like the name implies, transforms an object to a byte pattern and can recreate an object from a given byte-array (or throw some kind of exception if not).顾名思义,它将 object 转换为字节模式,并可以从给定的字节数组重新创建 object(如果不是,则抛出某种异常)。 I like the toBytes() method and it must be non-static, because it can only be applied to objects.我喜欢 toBytes() 方法,它必须是非静态的,因为它只能应用于对象。 But how can I create a static method so that I can call但是我怎样才能创建一个 static 方法以便我可以调用

ClassName.fromBytes(...);

How can I ensure, that every class implementing the interface has both of those functions or how can I create a static method that uses the non-static fromBytes(...) ?我如何确保实现该接口的每个 class 都具有这两个功能,或者如何创建使用非静态fromBytes(...)的 static 方法? Is it good practice to create a default constructor and the do something like创建默认构造函数并执行类似的操作是一种好习惯吗

new ClassName().fromBytes();

Is there any good practice?有什么好的做法吗? I guess an abstract class isn't a solution either, because it can not have static methods, can it?我想抽象的 class 也不是解决方案,因为它不能有 static 方法,可以吗?

you can create an abstract class that will contain your both method as abstract and you will extend that abstract class in your desired class.您可以创建一个抽象 class ,它将包含您的两种方法作为抽象,您将在所需的 class 中扩展该抽象 class。

    public abstract class Byteable<T> {
      abstract byte[] toBytes();
      abstract T fromBytes(byte[] bytes); 
    }

   public YourClass extends Byteable<YourClass>{
     //provide implementation for abstract methods
     public byte[] toBytes(){
        //write your conversion code 
     }
     public YourClass fromBytes(byte[] bytes){
        //write your conversion code 
     }
   }

Now you can create an object of your class and use both the methods.现在您可以创建 class 的 object 并使用这两种方法。

YourClass obj = new YourClass();
obj.toBytes()
obj.fromBytes(....)

And you can do the same with any desirable class just need to implement or give functionality to class to perform these tasks.您可以对任何所需的 class 执行相同的操作,只需为 class 实现或赋予功能以执行这些任务。

Other than this you can write one generic transform class:除此之外,您可以编写一个通用变换 class:

public class Byteable<I,O> {
  O toBytes(I obj){
    // Your transformation code
  }
  T fromBytes(O bytes){
    // Your implementation
  } 
}

Byteable<YourClass,byte[]> transformer = new Byteable<YourClass,byte[]>(); 
transformer.toBytes(new YourClass())
transformer.fromBytes(....);

I hope it's helpful for you.我希望它对你有帮助。

What you're trying to do is an abstract static method and that isn't allowed in Java.您正在尝试做的是一个抽象的 static 方法,这在 Java 中是不允许的。 Check this .检查这个

Besides that problem you can try this way.除了这个问题,你可以尝试这种方式。

public interface Byteable<T> {
    byte[] toBytes();
    Builder<T> builder();

    interface Builder<T> {
        T fromBytes(byte[] bytes);
    }
}

In this case you need to implement two interface 's.在这种情况下,您需要实现两个interface An example would be like this.一个例子是这样的。

public class Student implements Byteable<Student> {
    public final String name;

    public Student(String name) {
        this.name = name;
    }

    @Override
    public byte[] toBytes() {
        return name.getBytes();
    }

    @Override
    public Builder<Student> builder() {
        return bytes -> new Student(new String(bytes));
    }

    public static void main(String[] args) {
        Student s1 = new Student("Ana");
        Builder<Student> builder = s1.builder();
        byte[] bytes = s1.toBytes();
        Student s2 = builder.fromBytes(bytes);

        System.out.println(s2.name);    // Outputs Ana
    }
}

Note:笔记:

Is there any good practice?有什么好的做法吗? I guess an abstract class isn't a solution either, because it can not have static methods, can it?我想抽象的 class 也不是解决方案,因为它不能有 static 方法,可以吗?

You can have static methods in an abstract class.您可以在抽象 class 中使用static方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM