The presence of multiple "return types" in a Java method definition can seem puzzling. Let's delve into an example:
publicList getResult(String s);
where Foo is a class you have defined.
Question: What is the Return Type of This Method?
Answer: This method has a single return type: List
Question: Why Does It Seem to Have Two Return Types?
Answer: The method utilizes generics, a powerful feature in Java that allows for the creation of methods that work with different types by using type parameters. In this case:
In essence, the method declares that it can work with any type that extends Foo and returns a list of such types. This provides flexibility and type safety, ensuring that the returned list contains only objects that are subclasses of Foo.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3