英文:
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<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;
}
}
A simple test succeeds:
void testRestrictedGenericsType() {
RestrictedClassType<String> restrictedClassType = new RestrictedClassType<>();
restrictedClassType.storeLocal( "String-1");
System.out.println( "Restricted class: " + 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<T> {
T localVar;
public <S extends TypeWithGetAndSetMethod<T>> 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<String> restrictedClassType = new RestrictedClassType<>();
restrictedClassType.storeLocal( "String-1");
ProcessRestrictedGenericsType<TypeWithGetAndSetMethod> classRestrictedMethod = new ProcessRestrictedGenericsType<>();
classRestrictedMethod.aMethodWithRestrictedGenericType( restrictedClassType);
System.out.println( "The output is: " + 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<T, W extends TypeWithGetAndSetMethod<? extends Value>> {
WlocalVar;
public void procesTypeWithGetAndSetMethod(Warg) {
this.localVar = arg;
}
public T getRestrictedType() {
return localVar.getLocal();
}
}
Then your test will work:
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());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论