“This is the third day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.

The vast sea of millions, thank you for this second you see here. I hope my article is helpful to you!

May you keep your love and go to the mountains and seas in the coming days!

Foreword: Yesterday we talked about the concept of generics and some of the benefits of using them! But aren’t you familiar with how generics are used?

So, we are going to expand the use of generics one by one!

😒 Use of generics

Generics are often used heavily in collections, but we can also learn about generics in their entirety. Generics can be used in three ways: generic classes, generic methods, and generic interfaces. Pass the data type as a parameter.

Today we will focus on learning how generics are used to define how classes will be used.

😓 generic class

Generic types are used in the definition of classes and are called generic classes. Generics make it possible to open the same interface to operations on a set of classes. The most typical are the collection framework container classes: List, Set, Map.

  • Generic class definition format:

    Class class name < variables representing generics > {}Copy the code

    In case you’re not sure how to use it, I made a simple generic class:

    /** * @param <T> <T> <T> <T> <T> * Determine the specific data type of the generic at the time of use. That is, the generic type is determined when the object is created. */ public class GenericsClassDemo<T> {// T this member variable is of type T, the type of T is specified externally private T T; Public GenericsClassDemo(t t) {this.t = t; // GenericsClassDemo(t t) {this.t = t; Public T getT() {return T;} public T getT() {return T; }}Copy the code

    Generics are not concrete when they are defined, but concrete when they are used. Determine the specific data type of the generic at the time of use. That is, determine the generic type when creating an object.

  • For example: Generic

    genericString = new Generic

    (“helloGenerics”);

    In this case, the generic identifier T is of type String, so we can think of the class we wrote earlier as follows:

    public class GenericsClassDemo<String> { private String t; public GenericsClassDemo(String t) { this.t = t; } public String getT() { return t; }}Copy the code

    This is also handy when your generic type wants to change to an Integer type. T = T; T = T; T = T;

    Generic<Integer> genericInteger = new Generic<Integer>(666);

  • Note: to define a generic class, do WE have to pass in a generic type argument?

    This is not the case. When you use generics, if you pass in a generic argument, the generic argument will be restricted accordingly, and the generics will do what they are supposed to do. A method or member variable defined using a generic type in a generic class can be of any type, provided that no generic type argument is passed. That is, as in the classic case before, there is no generic type to write ArrayList, which is prone to type overcasts.

🌸 summary

I believe that you have a preliminary understanding of the use of generics, know how to use generics to define classes, so we continue to look forward to the next chapter on the use of generics. Welcome to the next chapter!

Let’s refuel together, too! I am not just, if there is any missing, wrong place, also welcome you to criticize in the comments of talent leaders! Of course, if this article is sure to help you a little, please kindly and lovely talent leaders to give a thumb-up, favorites, one key three even, thank you very much!

Here, the world is closed for today, good night! Although this article is over, I am still here, never finished. I will try to keep writing articles. The coming days are long, why fear the car yao ma slow!

Thank you all for seeing this! May you live up to your youth and have no regrets!