将C#中的通用类扩展的非通用类转换为Java - java

我正在尝试将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?

Result.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;
        }
    }
}

更新:特别感谢以下@JuanCristóbalOlivares的回答!以下是我的更改:

注意:我不得不将类型化的failok函数分别重命名为failTokT,因为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

请参见What's the meaning of “apostrophe + number” in the object type of properties with generics (eg. “Collection`1”)?。

因此,虽然非泛型类的名称为SomeClass,但泛型版本为SomeClass`1SomeClass`2等(取决于泛型参数的数量)。

Java泛型是不同的。泛型信息在编译时被删除。

请参见Are generics removed by the compiler at compile time。

这意味着非通用版本和通用版本只是同一类(SomeClass)。

因此,对于此用例,您可能只需要定义通用版本。此版本适用于一般情况和非一般情况。

无法从ArrayList <String>转换为List <Comparable> - java

当我写下面的代码时,编译器说 无法从ArrayList<String>转换为List<Comparable>private List<Comparable> get(){ return new ArrayList<String>(); } 但是当我用通配符编写返回类型时,代码会编译。private List&l…

合并List <T>和List <Optional <T >> - java

鉴于: List<Integer> integers = new ArrayList<>(Arrays.asList( 10, 12 )); List<Optional<Integer>> optionalIntegers = Arrays.asList( Optional.of(5), Optional.em…

实例化类型<?>的泛型类 - java

我正在为SCJP / OCPJP学习,并且遇到了一个对我来说很奇怪的示例问题。该示例代码实例化了两个通用集合:List<?> list = new ArrayList<?>(); List<? extends Object> list2 = new ArrayList<? extends Object>(); …

List <Dog>是List <Animal>的子类吗?为什么Java泛型不是隐式多态的? - java

我对Java泛型如何处理继承/多态感到困惑。假设以下层次结构-动物(父母)狗-猫(儿童)因此,假设我有一个方法doSomething(List<Animal> animals)。根据继承和多态性的所有规则,我假设List<Dog>是List<Animal>,而List<Cat>是List<Animal&g…

在HashMap <String,String>上循环时出现问题 - java

我有一个基本的HashMap。我正在尝试遍历它,并从Map中获取键和值。这是我所拥有的:Map<String, String> myMap = versionExtractor.getVersionInfo(); for(String key : myMap.keySet()) System.out.println(key); System.ou…