简体   繁体   中英

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

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?

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM