简体   繁体   中英

<T> cannot be resolved to a type

I want to change the return type of this method to a class ( ReturnContainer ) that can hold the tupelo object in addition to another return value that I need to send back to the calling method.

I've never worked with this <T> concept in Java before so I don't how to reconfigure this method to work the way I need it to.

public static <T extends LocationCapable> List<T> test(Class<T> incomingClass)
{
    List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
    return tupelo;
}

When I try to change the code to the listing below, I get the error:

T cannot be resolved to a type

How can I have a return type of ReturnContainer but still allow the incomingClass to be a dynamic type?

public static ReturnContainer test(Class<T> incomingClass)
{
            List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
            ReturnContainer rc = new ReturnContainer(tupelo, incomingClass);
    return rc;
}

You're missing the type parameter:

public static <T> ReturnContainer test(Class<T> incomingClass)
{
    List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
    ReturnContainer rc = new ReturnContainer(tupelo, incomingClass);
    return rc;
}

To my eyes, though, this looks weird. Shouldn't ReturnContainer have a type parameter, too? In which case, you'd have

public static <T> ReturnContainer<T> test(Class<T> incomingClass)
{
    List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
    ReturnContainer rc = new ReturnContainer<T>(tupelo, incomingClass);
    return rc;
}

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