你可以创建一个能够处理特定[受限]类型的泛型对象的类吗?

huangapple go评论102阅读模式
英文:

Java - How can I create a class that can process objects of Generics of a specific [restricted] type?

问题

如何创建一个能够处理特定类型泛型对象的类?

例如,我有一个基于泛型的类:

interface TypeWithGetAndSetMethod<T> {
    void storeLocal(T arg1);
    T getLocal();
}

class RestrictedClassType<T> implements TypeWithGetAndSetMethod<T> {
    T local;
    public void storeLocal(T arg1) {
        local = arg1;
    }
    public T getLocal() {
        return local;
    }
}

一个简单的测试成功通过:

void testRestrictedGenericsType() {
    RestrictedClassType<String> restrictedClassType = new RestrictedClassType<>();
    restrictedClassType.storeLocal("String-1");
    System.out.println("Restricted class: " + restrictedClassType.getLocal());
}

现在,我想要创建一个能够处理上述(泛型)类的类。我将它传递给一个处理类,该方法只能接受实现该接口的泛型类。

到目前为止,我有的代码无法编译:

// 不编译
class ProcessRestrictedGenericsType<T> {
    T localVar;
    public <S extends TypeWithGetAndSetMethod<T>> void procesTypeWithGetAndSetMethod(T arg) {
        this.localVar = arg;
    }
    public T getRestrictedType() {
        return localVar.getLocal();
    }
}

该处理代码的测试具有编译的代码:

void testMethodWithRestrictedGenericType() {
    RestrictedClassType<String> restrictedClassType = new RestrictedClassType<>();
    restrictedClassType.storeLocal("String-1");
    ProcessRestrictedGenericsType<TypeWithGetAndSetMethod> classRestrictedMethod = new ProcessRestrictedGenericsType<>();
    classRestrictedMethod.aMethodWithRestrictedGenericType(restrictedClassType);
    System.out.println("The output is: " + classRestrictedMethod.getRestrictedType());
}

你可以帮助我创建处理类吗?

英文:

How can I create a class that can process objects of Generics of a specific type?

For example, I have this class based on Generics:

interface TypeWithGetAndSetMethod&lt;T&gt; {
    void storeLocal(T arg1);
    T getLocal();
}

class RestrictedClassType&lt;T&gt; implements TypeWithGetAndSetMethod&lt;T&gt; {
    T local;
    public void storeLocal(T arg1) {
        local = arg1;
    }
    public T getLocal() {
        return local;
    }
}

A simple test succeeds:

void testRestrictedGenericsType() {
    RestrictedClassType&lt;String&gt; restrictedClassType = new RestrictedClassType&lt;&gt;();
    restrictedClassType.storeLocal( &quot;String-1&quot;);
    System.out.println( &quot;Restricted class: &quot; + restrictedClassType.getLocal());
}

Now I would like to have a class than can process the above (generic) class. So I pass it to a processing class and the method can only accept generic classes implementing the interface.

The code I have so far is not compiling:

// Not compiling
class ProcessRestrictedGenericsType&lt;T&gt; {
    T localVar;
    public &lt;S extends TypeWithGetAndSetMethod&lt;T&gt;&gt; void procesTypeWithGetAndSetMethod(T arg) {
        this.localVar = arg;
    }
    public T getRestrictedType() {
        return localVar.getLocal();
    }
}

The test of that processing code has compiling code:

void testMethodWithRestrictedGenericType() {
    RestrictedClassType&lt;String&gt; restrictedClassType = new RestrictedClassType&lt;&gt;();
    restrictedClassType.storeLocal( &quot;String-1&quot;);
    ProcessRestrictedGenericsType&lt;TypeWithGetAndSetMethod&gt; classRestrictedMethod = new ProcessRestrictedGenericsType&lt;&gt;();
    classRestrictedMethod.aMethodWithRestrictedGenericType( restrictedClassType);
    System.out.println( &quot;The output is: &quot; + classRestrictedMethod.getRestrictedType());
}

Can you help me creating the processing class?

答案1

得分: 1

以下是您要翻译的代码部分:

需要两个参数即值类型和包装类型

class ProcessRestrictedGenericsType<T, W extends TypeWithGetAndSetMethod<? extends Value>> {
    W localVar;
    public void procesTypeWithGetAndSetMethod(W arg) {
        this.localVar = arg;
    }
    public T getRestrictedType() {
        return localVar.getLocal();
    }
}

然后您的测试将会起作用

void testMethodWithRestrictedGenericType() {
    RestrictedClassType<String> restrictedClassType = new RestrictedClassType<>();
    restrictedClassType.storeLocal("String-1");
    ProcessRestrictedGenericsType<String, TypeWithGetAndSetMethod<String>> classRestrictedMethod = new ProcessRestrictedGenericsType<>();
    classRestrictedMethod.procesTypeWithGetAndSetMethod(restrictedClassType);
    System.out.println("The output is: " + classRestrictedMethod.getRestrictedType());
}
英文:

You need both parameters, the value type and the wrapper type:

class ProcessRestrictedGenericsType&lt;T, W extends TypeWithGetAndSetMethod&lt;? extends Value&gt;&gt; {
    WlocalVar;
    public void procesTypeWithGetAndSetMethod(Warg) {
        this.localVar = arg;
    }
    public T getRestrictedType() {
        return localVar.getLocal();
    }
}

Then your test will work:

void testMethodWithRestrictedGenericType() {
    RestrictedClassType&lt;String&gt; restrictedClassType = new RestrictedClassType&lt;&gt;();
    restrictedClassType.storeLocal( &quot;String-1&quot;);
    ProcessRestrictedGenericsType&lt;String, TypeWithGetAndSetMethod&lt;String&gt;&gt; classRestrictedMethod = new ProcessRestrictedGenericsType&lt;&gt;();
    classRestrictedMethod.procesTypeWithGetAndSetMethod(restrictedClassType);
    System.out.println( &quot;The output is: &quot; + classRestrictedMethod.getRestrictedType());
}

huangapple
  • 本文由 发表于 2020年8月8日 21:29:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63315962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定