我正在尝试将 C# 中的以下类转换为 Java。
Result
是一个由泛型 Result<T>
扩展的非泛型类类。
它们的示例用法如下:
// When we only care if the operation was successful or not.
Result result = Result.OK();
// When we also want to store a value inside the Result object.
Result<int> result = Result.OK<int>(123);
在 Java 中,每个类都需要在自己的文件中定义(除非它们是嵌入的)。
不幸的是,我找不到一种方法让基类和扩展类像 C# 中那样共享相同的名称。
有没有办法将以下 C# 代码转换为 Java 代码?
结果.cs:
using System;
namespace MyProject
{
public class Result
{
private bool _isSuccess;
private string _errorMsg = "";
public bool IsSuccess()
{
return _isSuccess;
}
public bool IsFailure()
{
return !_isSuccess;
}
public string ErrorMsg()
{
return _errorMsg;
}
public Result(bool isSuccess, string errorMsg)
{
bool errorMsgIsEmpty = string.IsNullOrEmpty(errorMsg);
if (isSuccess && !errorMsgIsEmpty)
{
throw new Exception("cannot have error message for successful result");
}
else if (!isSuccess && errorMsgIsEmpty)
{
throw new Exception("must have error message for unsuccessful result");
}
_isSuccess = isSuccess;
if (!errorMsgIsEmpty)
{
_errorMsg = errorMsg;
}
}
public static Result Fail(string errorMsg)
{
return new Result(false, errorMsg);
}
public static Result<T> Fail<T>(string errorMsg)
{
return new Result<T>(default(T), false, errorMsg);
}
public static Result OK()
{
return new Result(true, "");
}
public static Result<T> OK<T>(T value)
{
return new Result<T>(value, true, "");
}
}
public class Result<T> : Result
{
private T _value;
public T Value()
{
return _value;
}
public Result(T value, bool isSuccess, string errorMsg) : base(isSuccess, errorMsg)
{
_value = value;
}
}
}
<小时/>
更新:特别感谢下面的@Juan Cristóbal Olivares 的回答!以下是我的更改:
注意:我必须重命名键入的 fail
和ok
功能为failT
和okT
分别是因为 Java 不允许仅返回类型不同的函数。
Result.java:
public class Result<T> {
private boolean isSuccess;
private String errorMsg = "";
private T value;
public boolean isSuccess() {
return isSuccess;
}
public boolean isFailure() {
return !isSuccess;
}
public String errorMsg() {
return errorMsg;
}
public T value() {
return value;
}
public Result(boolean isSuccess, String errorMsg) throws Exception {
boolean errorMsgIsEmpty = StringUtil.IsNullOrEmpty(errorMsg);
if (isSuccess && !errorMsgIsEmpty) {
throw new Exception("cannot have error message for successful result");
} else if (!isSuccess && errorMsgIsEmpty) {
throw new Exception("must have error message for unsuccessful result");
}
this.isSuccess = isSuccess;
if (!errorMsgIsEmpty) {
this.errorMsg = errorMsg;
}
}
public Result(T value, boolean isSuccess, String errorMsg) throws Exception {
this(isSuccess, errorMsg);
this.value = value;
}
public static Result<?> fail(String errorMsg) throws Exception {
return new Result<>(false, errorMsg);
}
public static <T> Result<T> failT(String errorMsg) throws Exception {
return new Result<T>(null, false, errorMsg);
}
public static Result<?> ok() throws Exception {
return new Result<>(true, "");
}
public static <T> Result<T> okT(T value) throws Exception {
return new Result<T>(value, true, "");
}
}
示例用法:
// When we only care if the operation was successful or not.
Result<?> result = Result.ok();
// When we also want to store a value inside the Result object.
Result<Integer> result = Result.okT(123);
最佳答案
当您创建带有 1 个泛型参数的 C# 泛型类时,会生成该类:
SomeClass`1
因此,虽然非泛型类名为 SomeClass
,但泛型版本为 SomeClass`1
、SomeClass`2
等(取决于通用参数的数量)。
Java 泛型是不同的。通用信息在编译时被删除。
参见Are generics removed by the compiler at compile time .
这意味着非泛型和泛型版本只是同一个类 (SomeClass
)。
因此,对于此用例,您可能只需要定义通用版本。此版本适用于通用和非通用情况。
关于java - 将 C# 中的泛型类扩展的非泛型类转换为 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59745010/