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

Source: Internet
Author: User

A.

The basic use of generics has already been said, and the embodiment of generic classes, generic interfaces, and generic methods has been described. Now tell me about the advanced use of generics.

To a string that defines a collection, all of which are stored in strings, then we define the type. When you are clear, take a package and use the Add method to pass a few strings inside.

It is then traversed, and the type of traversal is also a string,

It's easy to run without problems.

But there is a problem, the collection is not unique. The fact is to introduce a variety of situations, to show the use of things, this is the use of the display of generics.

There are many instances where a collection is present, and how to iterate over one of the collections.

It is not appropriate to continue writing iterators because the container is constantly changing, the elements are constantly being added, but how the iterations are the same, in order to improve the reusability of code.

Regardless of which collection is iterated with this method, this method is used to print the function of the collection (iterating over and printing the elements in the collection).

It is also possible to make a HashSet collection without ArrayList the collection, but this method can only print list or ArrayList.

If the ArrayList and HashSet exist at the same time, then the method uses collection, improve the reusability, the expansibility is much higher.

collection level is relatively high, can undertake list or hashset. The above example, although the collection is different, but the types of storage elements are the same, the following look at the storage of different kinds of situations.

In this way al2 can not be passed to the inside, if we write two methods, display error, because the parameter type is consistent is the set, are collection. These two methods cannot exist at the same time, because all the received is a collection, what should I do?

This means that the types of elements stored in the collection of methods are not deterministic. When the type of operation is uncertain, it is not ready to use a specific type of case, such as not to use string,

If it's all string, that's the way it's written. However, we do not intend to use string, or not necessarily string, can be represented in the form of a different wildcard character.

Now we are going to introduce the generic wildcard characters, all matching symbols, this wildcard is the question mark?, location type.

cannot be explicitly typed, or write It.next ().

When we write a question mark, we can pass everything. There is another way, is not to write, this is also possible. The reason is: there is no generics in the old version, the method defined in the old version does not have generics, but we use the new version of the collection, it must be compatible, but the left side will alert the alarm, or do not write. Run it, there is no problem with the program.

The above is the use of wildcard characters, and again, this question mark is similar to the one written later. An unknown type is when it refers to a question mark that is indicated when the type is ambiguous.

Some people say, we can not do this, the difference is not big, the question mark to the letter, run as is possible.

A little different, if you write with T, it can be written in the following form, when the program is running, as long as there is a type has, is a unified type. But this method is static, do not need to clear the object, direct invocation can be, this and the question mark is not very different. Is it necessary to write so much trouble? No.

What is the difference? If you specify T, it means that the t can represent a specific type and can be manipulated. The specific display is as follows, when some of our tool classes have return values of T, this type can be manipulated, and for question marks, only the ambiguous type does not operate on the type, denoted by a question mark. Like this is just printing, you can use the expression of the question mark, there is no need to write this complex code of letters, and finally call the ToString method of object, which is the character of the question mark wildcard character.

The above is a simple use of wildcards, described below for its advanced usage. (The introduction of these knowledge points is based on the previous knowledge, a process of development)

In the example, the object worker is passed to the collection, and the ToString method in the worker class is covered for the post-printing effect. At the same time, the student class also make the same changes, because it is inherited, so you can use getname such a method. This is a parent class, not a private one,

The rest of the question mark is unchanged, and the final result shows that both the worker and the integer are printed, and any type can

Now we have a requirement, there are two sets, except that the worker is student,

Now explore the situation when the worker class and string exist together.

Now you want to print an element in a container, but this container has to have a condition, not print everything, just print the subclass of the person. Neither prints a class (if it is a class, then T can be explicit), and if all classes are printed, a question mark is indicated. Now you want to print a category, print worker, and student. Some people say that it is possible to use person directly, but the small icon on the left shows that it is wrong.

The person is a separate type, and the worker and student are separate types, and there is an inheritance relationship between them, but here collection can only store the person's object, which is a concrete type.

For example, the full write is as follows, the generics on both sides of the left and right do not match. Now there is a set of Al, if they can be expressed by the inheritance relationship, when the collection declaration said that all of them are human, that is, both the worker and student can be stored in. But the entity on the right is marked only as student, which creates a contradiction (what is the difference between polymorphism and generics at this point?). )。 In general, writing generics should have the same characteristics as the left and right sides.

But we now want to receive workers and student, to receive the subclass object of person or person, you can do this. The person sub-class object is many, is not sure, the wording is as follows. There are also wavy lines here, and a bag can be made.

The above-mentioned knowledge point is the generic qualification, which belongs to the advanced application of generics. Before using object, just write an object, then anything can be solved, now do the qualification, only a part of the type can come in, if not this type or its subclass, then the direct compilation fails. Before it was compiled nothing, run out of trouble.

Many of the updates in Java now are run-time errors that go to compile time, which is more secure.

The generic type of the qualified knowledge point is specifically written as follows, after qualification, you can directly take the inheritance method. The compile-time detection is all right, as long as it is a subclass of its being stored in. When you get it, you can use the parent class to take it, and you can't use student to get it, because it's a question mark, and the first embodiment of the generic qualification is taken care of. At the time of traversal, there seems to be a question of whether the parent method can be called directly.

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

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.