![]() ![]() For more information about Java’s naming conventions for type parameters, see the section, Type Parameter Naming Conventions in the Generic Types tutorial. ConventionsĪlthough technically any valid identifier would work as the name of a type parameter, by convention, type parameter names are often a single, upper-cased character, just like in Java. To see examples of those, check out the article dedicated to type parameter declarations. Then the the type parameter is referenced as the type of item (which also has a public getter and setter).Ĭlasses, functions, extension properties, and interfaces can all be generic. ![]() Here we’ve got a simple declaration where we tell the compiler that this class is a generic with a type parameter called T. This is one of the simplest examples of a generic - it’s a class that wraps any object. Within your generic, you first declare the type parameter, and then you can reference it in multiple places throughout the function, class, or interface. Just as you can choose from a variety of different light bulbs to put into that socket, the calling code can choose from a variety of different types to pass to a generic. If you imagine your generic as a light fixture, the type parameter would be the socket where the light bulb goes. A type parameter is a placeholder for an actual type, which gets specified by the code that’s using the generic.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |