Alibabacloud.com offers a wide variety of articles about implement interface with generics java, easily find your implement interface with generics java information here online.
> Generics: Generic refers to the typed type of the parameter, which is commonly used in interfaces, classes, methods> function: Used to determine the range of parameters, in writing code in advance to check the code error> generic declaration, which gives the class declaration, and so on:Class classnameClass classname> General character:> Qualifier: 1) Qualifier letters can only be limited once2) When extends can only use capital letters, when super
Public list
List
The bank is a class that contains SETDH (), setd (), Setkubh (), SETXM (), SETZJLX () method, so you can invoke these methods when you instantiate the bank. And the list is stored in the bank type of lists, and list is a Java collection, it is an abstract interface, to achieve its arraylist,linkedlist and so on.
Generics are the types that are r
Replacement PrincipleBased on some object-oriented features of Java, we can easily understand such a replacement principle:
A variable of the specified type can be assigned to any subclass of this type. A method that specifies a type parameter can be called by passing in a subclass of this type.
In general, that is to say, any type variable we use can be replaced by the Child type of this type.
An incorrect inheritance relationship in genericsIn gener
=L1.next (); if(Obj.equals ("List") ) {l1.remove (); L1.add ("List02"); }} System.out.print ("Collection: \n\t" +a1+ "\ n"); }Running, we can see:Collection: [List01, List02, List03, List04]This allows us to successfully resolve this concurrency modification exception. Remove the ' List ' element and add a ' List02 ' element.Vectors are very similar to ArrayList. As early as the JDK1.1, there was no known list interface, and now this class has been
Differences between super T and extends T in Java generics
The declaration of List
Extends
The wildcard declaration of List
// Number "extends" Number (in this context)
List
// Integer extends Number
List
// Double extends Number
List
With the given value assignment statement, what type of elements can be read from the foo3 list? You can read the Number, becaus
The example in this article summarizes the usage of generics in Java. Share to everyone for your reference. Specifically as follows:
1 Basic use
Public interface List
2 Generics and subclasses
The child is a subclass of Parent,listtherefore:listIf the above is correct, then:
list
3 wildcards
We all know that Java source code will erase the generic information after compiling, now see a generic type is not erased, see the Concurrenthashmap.comparableclassfor method.Parameterizedtype.getactualtypearguments can get generic information that is not erased in the interface implemented by the class. The experiment is as follows: Public classParameterized { Public Static voidMain (string[] args) {type[
When we declare a generic interface or class, or need a subclass to inherit to this generic class, we want to get these generic parameter information using reflection. This is the reflectionutil to be introduced in this article is to solve this kind of problem of the Auxiliary tool class, for the Java.lang.reflect Standard Library of the tool class. It provides a convenient way to access the reflection method of the Generic object type (Java.reflect.T
Note: class is a Java keyword that is used when declaring a Java class;
Instances of class classes represent Java Application Runtime classes (class ans enum) or interfaces (interface and annotation) (each Java class runtime behaves as a class object in the JVM, The
Summary:This section describes several usage methods of generics.
Definitions of simple classes and interfaces:
public class Pair
public interface ICache
PS: type variables are in uppercase and are short. In the Java library, variable e Represents the elements of the Set, and K and v represent the types of table keywords and values respectively. T indicates "an
The use of generics guarantees that the returned object type is correct; Packagecom.stono.gentest;Importjava.util.ArrayList;Importjava.util.List; Public classStoryteller { Public Static voidMain (string[] args) {List, NewStoryi() {@Override PublicMinstrel Gethero (String name) {return NewMinstrel (name); } }, 1); for(Minstrel minstrel:story) {System.out.println (Minstrel.getname ()); } }}classMinstrel {PrivateString n
First, introduce
1, the generic is what first tell everyone arraylist is generic type. That ArrayList can accomplish what unexpected function. Let's take a look at the following code:
[Java]View plain copy arraylist 2, no generics will doLet's look at the following code:We implement two classes that can set the point coordinates, and set the point coordinates
use the extends keyword to restrict generic class objects, the compilerBy default, all subclasses of the object class can be instantiated.. That is:
public class LimitDemo
It is equivalent:
public class LimitDemo
2. Type wildcard
If there is a demo with a reference name, we hope that the demo can reference both the wilddemo
If no wildcard is used, we can only define two variables: wilddemo
// Use the wildcard '? 'Wilddemo
However, note that,An object referenced by a name declared by a typ
T range:
In fact, when we don't use the extends keyword to restrict generic class objects, the compilerBy default, all subclasses of the object class can be instantiated.. That is:
Public class limitdemo
It is equivalent:
Public class limitdemo
2. Type wildcard
If there is a demo with a reference name, we hope that the demo can reference both the wilddemo
If no wildcard is used, we can only define two variables: wilddemo
// Use the wildcard '? 'Wilddemo
Source: 1190000002646193Generic class Public classContainer { PrivateK Key; PrivateV value; PublicContainer (k K, v V) {key=K; Value=v; } PublicK GetKey () {returnkey; } Public voidSetkey (K key) { This. Key =key; } PublicV GetValue () {returnvalue; } Public voidSetValue (V value) { This. Value =value; }}Generic interface Public Interface Generator { public T Next ();} Public class I
MyClass (t newt) {4this . t = Newt; 5 } 6 }About the generic definition of the data:1 @SuppressWarnings ("Unchecked")2 myClass (int maxsize) {3 This . Tlist = (type[])new object[maxsize]; 4 }2, the knowledge point 2--the generic type instantiationHere you need to explicitly indicate the data type1 New myclassSecond, generic interface:1 Public InterfaceMyinterface{2 PublicT Next ();3 }4 //
, String > MyMap = new HashMap IntegeR , String>();
Set MYCLaSS > set = new HashSet Myclass>(0);
No more examples, these examples are straightforward enough.
at the end of this article, let's go back to the procedure to clear a single, have not previously warned? If you are such a person who does not want to use type safety to eliminate the warning, and do not want to see that nasty Waining, you can add such a sentence before the Main method (if you use IDE can
understand the problem ----- generic. For our example, Java 5's type security has to play a role.
Now let's go back to program list 1, open your development tool, compile it (eclipse and other tools can be seen without your own variation), and we find that the program has no errors, however, there is a warning:
Type Safety: The method add (object) belongs to the raw type list.
References to generic type list shocould be par
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.