Java接口静态方法解决方法? [英] Java interface static method workaround?

查看:128
本文介绍了Java接口静态方法解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个给定的REST接口:

We have a given REST interface:

POST /calculation
<data>abc</data>

此计算可以由不同的逻辑计算器实现,具体取决于服务器配置。

This calculation can be implemented by different logical "calculators" depending on the server config.

我们现在正在设计每个计算器必须实现的Java接口。该接口将为每个REST服务提供一种方法。

We are now designing the Java interface that each calculator must implement. The interface will have a method for each REST service.

鉴于所有REST(和HTTP)调用都是无状态的,每个方法都应该是静态的。但是,您无法在Java接口中定义静态方法。这种情况有一个很好的解决方法吗?

Given that all REST (and HTTP) calls are stateless, each method should be static. However you can't define static methods in Java interfaces. Is there a good workaround for this situation?

我们可以将方法定义为非静态,然后首先创建一个计算器类的实例。通过在接口中使用static关键字来指示方法是无状态的似乎更清晰。

We could define the methods as non static and then just first create an instance of the calculator class. It just seems cleaner to indicate that the methods are stateless by using the static keyword in the interface.

推荐答案

在我看来你想要一个接口与声明为normal的方法和一个实现类,然后简单地实例化它的一个实例。 本身没有理由让方法本身是静态的。

It seems to me that you want an interface with the methods declared as normal, and an implementing class, and then simply instantiate a single instance of this. There's no reason per se for the methods themselves to be static.

这篇关于Java接口静态方法解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆