Generics Types learning notes <one>

zhaozj2021-02-11  236

Generics Types generic learning notes 1

Author: ice clouds icecloud (AT) sina.comBLOG: http://icecloud.51.net

Time: 2004.02.15

Copyright Notice:

This article is completed by ice cloud, started 9CBS, the author retains Chinese copyright. No commercial use is not permitted without permission. Welcome to the reprint, but please keep the article and copyright statement complete. For contact, please send an email: Icecloud (at) sina.com

Java 1.5 provides generic support. A few days ago, Sun released a TUTORIAL. The following is a learning note on the tutorial.

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

Generics in the Java Programming Language

Gilad Bracha

Febrary 16, 2004.

1 After the generic compilation, it will actually produce different types declarations.

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

Basic call

List Myintlist = new linkedList (); myintlist.add (new integer (0)); integer x = myintlist.ITeger (). Next ();

Note 1: Each generic statement is only compiled once and generates a separate file for each different type. Just like the old class. For example, there may be list and list classes are generated.

Original: a Generic Type Declaration IS Compiled Once and for ALL, AND TURNED INTO A Single Class File, Just Like An Ordinary Class or Interface Declaration.

After compiling, it is equivalent to the following class.

Public interface integerList {void add (integer x); item item ();}

2 List Not List subtype

The following statement will not be able to compile, an error occurs on line 2, because the LO is not the parent type of the LS.

List ls = new arraylist (); list lo = ls; lo.add (new object (); string s = ls.get (0);

Note 2: In general, if foo is a subtype of BAR, G is a generic type, then G is not a subtype of g . This is the biggest difficulty of generic learning.

Original: IN General, IF Foo Is A Subtype (Subclass Or Subinterface) of Bar, And G Is Som generic Type Declaration, IT IS Not The Case That G IS A Subtype of G .3 generic parent The type is , A wildcard type.

A generic method for collection is as follows, where the FOR cycle uses a new method:

Void PrintCollection (Collection c) ​​{for (Object E: c) System.out.Println (E);

Among them, Collection Indicates that it can be any type. Such as Collection , Collection , Collection Is their parent type.

Note 3: Collection Is all kinds of subclasses. Instead of Collection . This is called "Wildcard Type" wildcard type.

Original: What is the supertype of all kinds of collections It's written Collection (pronounced "collection of unknown"), that is, a collection whose element type matches anything It's called a wildcard type for obvious reason? ..

4 Bounded Wildcards. Amountable wildcard type

Unfortunately, the following methods cannot be successful. Although each object in List is a shape type.

Public void Drawall (List Shapes) {for (SHAP S: Shapes) s.deaw ();} DrawAll (List Circles);

You can solve it through Bounded Wildcard: however, this will bring a little price. That is, it is read-only, you can't add new elements to the list.

Public void Drawall (List Shapes) {// The following call is wrong, you can only read Shapes.Add (0, new circle ());}

Note 4: is a limit for wildcard type, which can accept all and subtypes of Class. However, the call cost is read-only.

Original: ... We have replaced the type List with List Now drawAll () will accept lists of any subclass of Shape It is an example of a bounded wildcard It is now illegal to write into shapes in The body of the method.

This is about one-third, on the subway, and I haven't seen it.

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

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