简体   繁体   English

如何将两个返回类型不同但主体相同的方法与 throw 语句结合起来?

[英]How to combine two methods with different return type but same body with throw statement?

I have two methods with different data types, but with same body.我有两种具有不同数据类型但具有相同主体的方法。 I'm not using any return .我没有使用任何return I'm using these methods for throwing exceptions only.我使用这些方法仅用于抛出异常。 Is there any way to combine these two methods or any way to optimize the code?有没有什么办法可以结合这两种方法或者有什么方法可以优化代码?

public MyDataType1 method1(MyArg arg){
    /* some logic */
    throw new CustomException();
}
public MyDataType2 method2(MyArg arg){
    /* some logic, same as above method logic*/
    throw new CustomException();
}

PS: I don't want to use interface or some new method with common logic. PS:我不想使用接口或一些具有通用逻辑的新方法。

Use an interface to make compatible使用接口使兼容

interface DataType{
}

class MyDataType1 implements DataType{
}

class MyDataType2 implements DataType{
}

You'll be able to你将能够

public DataType method1(MyArg arg){
    /* some logic */
    throw new CustomException();
}

It is a little-known language feature called a 'method' :)这是一个鲜为人知的语言功能,称为“方法”:)

public MyDataType1 method1(MyArg arg){
    logic();
    throw new CustomException();
}

public MyDataType2 method2(MyArg arg){
    logic();
    throw new CustomException();
}

private void logic() {
    /* some logic */
} 

I have two methods with different data types, but with same body.我有两种方法,它们的数据类型不同,但正文相同。 I'm not using any return .我没有使用任何return I'm using these methods for throwing exceptions only.我正在使用这些方法仅引发异常。 Is there any way to combine these two methods or any way to optimize the code?有什么方法可以将这两种方法结合起来,也可以通过什么方法来优化代码?

public MyDataType1 method1(MyArg arg){
    /* some logic */
    throw new CustomException();
}
public MyDataType2 method2(MyArg arg){
    /* some logic, same as above method logic*/
    throw new CustomException();
}

PS: I don't want to use interface or some new method with common logic. PS:我不想使用具有通用逻辑的界面或某些新方法。

暂无
暂无

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

相关问题 有没有办法组合两个具有相同主体和返回类型但参数不同的方法? - Is there a way to combine two methods that have the same body and return type but different parameters? 如何为同一方法传递不同的 object 类型,而不是在 java 中编写具有相同功能和返回类型的两个方法 - How to pass different object type for same method instead of writing two methods with same functionality and return type in java 两个接口中具有相同签名但返回类型不同的方法 - Methods with the same signature but different return type in two interfaces 如何在同一条语句中合并两个SUM查询? - How to combine two SUM queries in the same statement? 如何合并两种执行相同操作但类型不同的方法? - How can I combine two methods that do the same thing but differ in type? 如何将两种方法结合在一起使用相同的计算但字段不同 - How to combine two methods with identical calculates but different fields 接口的java类型作为方法参数-如何将两个方法组合为一个 - java type of interface as method parameter - how to combine two methods into one Java 8 - 两个接口包含具有相同方法签名但返回类型不同的默认方法,如何覆盖? - Java 8 -Two interfaces contain default methods with the same method signature but different return types, how to override? 如何在JAVA中结合两种方法? - How to combine two methods in JAVA? Spring MVC:如何在 ResponseEntity 主体中返回不同的类型 - Spring MVC: How to return different type in ResponseEntity body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM