JDK 1.5 introduced new features - generics

xiaoxiao2021-03-06  81

1 Introduction

New language components are introduced in JDK 1.5, generics is one of them. Simple generic (Defining Simple Generics)

The following code is taken from the List interface of the java.util package and the definition of the Iterator interface:

Public interface list {void add (e x); item item ();} public interface iterator {e next (); boolean hasnext ();}

Type parameters

Some things related to polar brackets are new things introduced by JDK 5, they are the "formal type parameters" of the List and Iterator interfaces ("Types") declaration. When the generic declaration list is called (for example : List ), all types of types (such as e), will be replaced by "actual type parameters" (referred to as "type", such as Integer).

Although the template mechanism in C is imagined in the form, it is necessary to note that generic statements in Java will never be displayed in multiple copies when they call: whether they are in the source level, binary, or in disk or memory It will not be launched!

The generic statement will only be compiled once, and generate a class file (Class file), which is exactly the same as a normal class or interface.

Type parameters are actually similar to the usual parameters used in the method or constructor. A method can declare it "form value parameters", similarly, generic declarations also have its "type parameters"; When called, the actual parameter will replace the formal parameters, then perform the method body, the same, when the generic declaration is called, the actual type parameter replaces the type parameters of the form.

Remarks on naming conventions: Recommended types of type parameters in form using refined and concise (e.g., single characters). It is best to avoid using lowercase characters to distinguish between the parameters of ordinary classes or interfaces. Many promotional goods Types use e to indicate type of the type of its element.

2. Generals and Subtyping

First look at the following two lines of code is legal: List ls = new arraylist (); // 1List LO = LS; // 2 The first line is no problem, the key is in the second line of code, big Most people will think that "a String List is naturally an Object's list", so there is no problem with the second line.

Ok, then look at the following code: lo.add (new object ()); // 3String s = ls.get (0); // 4: Try to give an Object to a string! Visible, through alias LO, we can For LS, a String list, data operation (especially inserting an Object), resulting in LS not only to accommodate String objects! This is the Java compiler is not allowed! Compile, the second line will report a compilation error of.

Generally, if foo is a subtype (sub-class or sub-interface) of BAR, G is a generic statement, then G is not a subtype of g .

This is often the most difficult to understand because it is and the usual intuitive phase. In an intuitive understanding, we actually assume that the collection will not change, but this is not the case in the Java language.

3. Wildcards

Assume that all elements in a collection are output. The following is the following is the WITRY:

Void PrintCollection (Collection C) {Iterator i = C.Iterator (); for (k = 0; k

Void PrintCollection (Collection C) {for (Object E: C) {System.out.println (E);}} The problem is more useful than the old version. Because the old version can use various types The collection is used as a parameter, but the new version can only use Collection . As you can see, Collection is not a super-type (parent type) of other collections.

All types of intenses should be written: Collection , Read: Collection of unknown, a collection, the element type can match any type. Therefore, this type is called "wildcaries".

Correctly implement the old version of the code described above: Void PrintCollection (Collection C) {for (Object E: c) {system.out.println (e);}}

At this time, this method can be called with any type of collection. Note that in the method body, it is still read from the element and assigns Object, which is not wrong, so regardless of the type of arguments, it The elements are Object. However, if any gives an Object, it is unsafe:

Collection C = new arraylist (); c.add (new object ()); // Compile error

Since we don't know what is the element type of C, you can't add an object. Method Add () accepts a type E parameter, and E is the same as the elements type of the collection. When the type of arguments are, it means "it" Unknown type ", the parameters we pass to add must be this" unknown type "subtype. Unfortunately, since the type is unknown, you can't determine its child type, so you can't use it as a parameter. The only exception is Null, because null is a member of all types.

On the other hand, if you give a list <>> we can call the get () method and use the elements returned. Although the type of elements returned is "unknown type", it is always an Object, so it will be Get () The returned element is assigned to an Object type variable, or the parameters that pass it to a acceptable object are safe.

{TO BE Continued}

转载请注明原文地址:https://www.9cbs.com/read-107351.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.044, SQL: 9