Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java—如何创建一个类来处理特定[restricted]类型的泛型对象?_Java_Generics - Fatal编程技术网

Java—如何创建一个类来处理特定[restricted]类型的泛型对象?

Java—如何创建一个类来处理特定[restricted]类型的泛型对象?,java,generics,Java,Generics,如何创建一个可以处理特定类型泛型对象的类 例如,我有一个基于泛型的类: interface TypeWithGetAndSetMethod<T> { void storeLocal(T arg1); T getLocal(); } class RestrictedClassType<T> implements TypeWithGetAndSetMethod<T> { T local; public void storeLoca

如何创建一个可以处理特定类型泛型对象的类

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

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;
    }
}
接口类型与GetAndSetMethod{
局部无效(T arg1);
T getLocal();
}
类RestrictedClassType使用GetAndSetMethod实现类型{
T局部;
公共空存储本地(T arg1){
本地=arg1;
}
公共T getLocal(){
返回本地;
}
}
一个简单的测试成功:

void testRestrictedGenericsType() {
    RestrictedClassType<String> restrictedClassType = new RestrictedClassType<>();
    restrictedClassType.storeLocal( "String-1");
    System.out.println( "Restricted class: " + restrictedClassType.getLocal());
}
void testRestrictedGenericsType(){
RestrictedClassType RestrictedClassType=新的RestrictedClassType();
restrictedClassType.storeLocal(“字符串-1”);
System.out.println(“受限类:+restrictedClassType.getLocal());
}
现在我想要一个能够处理上述(泛型)类的类。因此,我将其传递给一个处理类,该方法只能接受实现该接口的泛型类

到目前为止,我的代码尚未编译:

// 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();
    }
}
//未编译
类ProcessRestrictedGenericsType{
T局部变量;
public void proceTypeWithGetAndSetMethod(T参数){
this.localVar=arg;
}
公共T getRestrictedType(){
返回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());
}
void testMethodWithRestrictedGenericType(){
RestrictedClassType RestrictedClassType=新的RestrictedClassType();
restrictedClassType.storeLocal(“字符串-1”);
ProcessRestrictedGenericsType classRestrictedMethod=新的ProcessRestrictedGenericsType();
classRestrictedMethod.MethodWithRestrictedGenericType(restrictedClassType);
System.out.println(“输出为:+classRestrictedMethod.getRestrictedType());
}

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

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

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

class ProcessRestrictedGenericsTypes如果您坚持Java代码约定并使用单个字母作为泛型类型参数,这将是一个更好的答案。使泛型类型看起来像类名是令人困惑的。@VGR我想说这个惯例已经过时了,因为我们有适当的IDE,它们以不同的方式突出显示类类型和参数类型。我不同意。编写难以阅读的代码是不可接受的,因为“IDE可以处理它”。@VGR在这种情况下,我同意使用单字母名称更容易阅读,因为整个类都可以放在屏幕上,但如果是大型类和具有两个以上类型参数的类,则这是有争议的。常规类型名(类似于类名)可以提供更深入的语义,并使其更容易理解所发生的事情。例如,谷歌的惯例既允许使用单字母名称,也允许使用以
T
结尾的类名称,因此我想说,相当一部分Java专业人士也有类似的观点。
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());
}