Java-preliminary Understanding-Common Object API (collection framework-generics-generic class)

Source: Internet
Author: User

One.

Generics after we understand the fundamentals, there are other applications in addition to passing parameter types in collections. It can also provide us with many design conveniences.

Describe the previous and present definition methods, what is different.

In order to facilitate the application of this, there is a tool class, and the main program sub-development.

For the description of the tool class, now to manipulate the person, to use the tool class to manipulate the person object, you can complete the person object settings and get.

This tool class can only manipulate the person and cannot manipulate other objects. If you want other objects, write tool1,tool2 ... This is very troublesome, how to do? (operate on the person object that is being transferred in)

You can not define a tool class to manipulate all objects. If you want to define a tool class and manipulate all objects, you should find the commonality of all objects, such as changing the person here to object.

If it is written as an object, it means that anything can be manipulated.

There is a shortcut to modify the operation as shown in.

Now to demonstrate, build students and workers, as well as the people of these three classes, students and workers are heirs to this class, which is the basis of the following demonstration. (These are all part of the generic main function)

Now create a tool class in the main function, then pass in a student class, and then remove the student class by Gerobject. The book shows a wavy line, indicating an error.

The reason for the error is that when the student is stored in the tool class, it means that the transformation is upward, and then the object class is taken out. If you want to take out a particular type, you must turn it.

The above is our previous practice, run a bit, no error.

Now carelessly, passing a worker class inside,

Go back to see the tool class, which is receiving the object class, so it is stored in the student class, or worker class, is possible. Compile without error, but the above program is running with errors.

This is what we used to do, in order to improve the extensibility of the program, we use all of the object, what can be passed to the inside. The advantage is that object appears, improve extensibility, but with the drawbacks, is to strong turn. Not strong turn can not be used.

Now make some changes to the tool loaded with object in the program, make a change.

Write the changes back and forth in a Java tool class, looking up and down. When writing a tool class, this is used to manipulate the object tool class, followed by which object you want to manipulate, not sure. It used to be an object, and now it has a generic type. Manipulate which object, you pass it to me, pass whichever, and manipulate which. Define the parameters above, which is 5.0.

Used to play Arralist is Java-defined, now this is what we define, this is called a custom generic class.

If you use object without generics, you can understand that generics are a scenario in object, but this is a more secure approach.

Now come back to the program that modifies the previous error,

If it is student from beginning to end, it is no problem to compile and run, but now I have accidentally entered the worker.

The above input worker's writing, if is the early wording, runs the time to have the error. Now to the generic type of writing, the compilation will not pass. This is the benefit of generics, which further verifies that generics will run as a problem and goes to the compile-time. Avoid the trouble of strong turn.

This is much safer than object, but it is difficult to write,

Java-preliminary Understanding-Common Object API (collection framework-generics-generic class)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.