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

Source: Internet
Author: User

One.

I just talked about the generic class, and now we're going to say another one, on the basis of the tool class (the generic tool class based on the previous section).

If you were to display and print a method of the string class, the arguments would directly indicate the string type, but if you wanted to display and print parameters of the Intrger type, you would need to change the parameter to object.

Based on generic tool classes, writing can be the following. What type is specified inside the tool class, and the method follows what type to manipulate.

Does this make sense, to demonstrate,

There is no problem passing the string type, but the generic in the tool class is the type of pass, so change to an integer and the integration tool shows the error.

Because the defined class is generic, and the object specified by the tool refers to a string, then the object can only manipulate any method that has a string argument. Once this method is clear, the show is already a string, then the other types will fail, because the type check does not pass, the compile time does not pass.

Now we want to show that there is any type of, how to do? This means that the object type of the Show method operation is indeterminate. It is possible to change the parameter type of the Show method in the tool class to object. Because these objects are ultimately called methods in object, the second one shows the polymorphism.

You can also do the second thing, if the case is ambiguous type, object is actually a concrete type. It can also be, what to pass inside, what to do, you can define generics, define generics on the method.

This is the definition of generics. (w str) is used when the parameter,<w> is in the definition parameter, only defined. Generics are used for definition?

After the generic is defined on a method, what type can show, and the source program compiles and runs without problems.

The print generic is followed by the object, and the show's generics are customized on the method.

The above program is then demonstrated, adding a static method. The integrated environment shows that compilation is wrong, compared to the previous print method, where there is a static modification.

Static is not required for objects, generics are required to be explicit. Static do not know how to use QQ, the solution of the same way, both static, but also method methods can structure a variety of types.

The basic use of generic methods is explained.

Again, if the Show method and the Print method receive a string, can the length of the string be printed? The following display is not possible, once the generic type is used, the variable type is indeterminate, how can you use a specific object method, in case the person type is passed, how can be written str.length (). But some methods of this variable are available,

What are the methods that can be used? Display it with a dot number. We know that no matter what type is passed in, it must be an object type, so it must have the method of object.

If the show or Print method is written directly into object, then only the object method can be used, but, if written in the form of generics, it will be explained later that the W type is available for an application.

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

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.