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.
many different types of objects.is an extension of the type system of the Java language to support the creation of classes that can be parameterized by type. The ability to think of type parameters as a placeholder for the type specified when using the type of the parameter. Just as the form parameter of a method is a worthy placeholder passed at execution time."Several types of Java
for a project. Before generics, there is also a better way of handling, such as Class GenericTest2, we can use the type of object (interface or parent class) to implement the processing of different types of data. But this is not perfect, first of all, the data sent in and the data must be consistent, even if not consistent, the compiler will not error or the ID
Generic Summary:1. Generics have the ability to parameterize types. You can define a class or method that uses a generic type, and the compiler replaces the generic type with a concrete type.2. The primary advantage of generics is the ability to detect errors at compile time and not at runtime.3. A generic class or method allows you to specify the type of object this class or method can carry, or if you att
(collectionBooleanRetainall (collectionvoidClear ();BooleanEquals (Object O);intHashcode (); E Get (int Index); E Set (int Index, E Element);voidAddint Index, E Element); E Remove (int Index);intIndexOf (Object o);intLastIndexOf (Object o); Listiteratorint Index); ListintFromIndex,intToindex);}We can see that after the generic definition in the list interface, E in,Naturally, ArrayList as the implementation class for the list
There are many reasons for the emergence of generics, and one of the most noticeable reasons is to create container classes .
Generic classThe container class should be one of the most reusable class libraries. Let's take a look at how a container class is defined without a generic type:PublicClassContainer {Private String key;private String value;PublicContainer(string k, String v) {key = k; value = V;}public String getKey () {return key;} publi
wants to join an integer object, and by List
In conjunction with the above generic definition, we know that in list
1 public Interface list
We can see that in the list interface, after the generic definition is adopted, the E in,
Naturally, ArrayList as the implementation class of the list interface, which is defined in the form of:
Thus, we understand from t
Use of generics in Java:1. Common generic UseThe type that follows public class Myclass12. With two generic typesAs with normal generics, it's just that we can define two different generic types when using classes, and of course we can define multiple as long as our business needs.public class Myclass23. Generics with
Generic typeI. Defining a generic classpublic class Pair{Private T _first;Public Pair (T first) {This._first = first;}Public T GetFirst () {return _first;}}Type variables in Java are used in uppercase and relatively shortE: element type k,v the keyword of the table and the value of type T (u/s) any typeGeneric method: Either the return type is generic or the parameter is genericpublic static {return A[A.LENGTH/2];}When called, the program automaticall
1. Why why--introduces a generic mechanismIf we want to implement a string array and require it to dynamically change size, then we all think of aggregating a string object with ArrayList. However, after a while, we wanted to implement an array of date objects that could be changed in size, and of course we wanted to be able to reuse the ArrayList implementation of the string object that was written before.
Java BASICS (II: generics and enumeration)1,Generic
Similar to object-oriented polymorphism, application generics can improve program reusability. Different from polymorphism, application generics can reduce data type conversion and improve code running efficiency. Generics
also be defined in normal classes. The type parameter T1 in the generic Method Printpoint (), T2 is not necessarily associated with the T1 in the generic class point, T2, or other identifiers can be used instead:
public static
Generic interface
A generic interface can also be defined in Java, where it is no longer redundant, just to produce sample
FruitextendsObject { }
Thus garage and garage are OK but garage is Not OK.You can specify more than one bound with extends:?
1234
classVehicle { }interface PassengerVehicle { }interface MotorVehicle { }class ParkingGarageextendsVehicle MotorVehicle PassengerVehicle>
You can specify at the very one class in the bound (obviously, as can only inherit from one class
For generics, you must know the class type. However, writing T. Class directly is obviously not acceptable. I checked a lot of information from the Internet and found only one result. Because Java's generic implementation uses the "wiping method" (I did not go into details, haha ), as a result, Java generics cannot directly obtain their declared
The motive of Java generics originThe simplest way to understand Java generics is to think of it as a handy syntax that can save you from some Java-type conversions (casting): List As the code above, it is not necessary for the programmer to do type judgment, because
You can use angle brackets with one or more arguments when declaring an interface and class, but when you declare a variable that belongs to an interface or class, or when you create a class instance, you need to provide their specific type. Let's take a look at the following exampleList ,arraylistList words = new ArrayList (); Words.add ("Hello"); Words.add ("world!"); String s = ((string) words.get (0))
understanding, see the following builder interface definition: Public Interface Generator { public T Next ();}Then define a generator class to implement this interface: Public class Implements Generator { privatenew string[] {"Apple", "banana", "pear"}; @Override public String Next () { new Rando
Understanding java-13.5 from the ground up using generics to build complex models
This section describes how to use generics to build complex models?
1. List of tuples
We have already said that tuples are a complex model that can return multiple objects.
Package com. ray. ch11; import java. util. ArrayList; public cla
Tag: The Nether OID should compile the object add generic Java generic type1. People who have a basic knowledge of Java know a little about generics, understand where they often appear and how to use them.On class: Class name I will not repeat it.2. Generics are changing the type into a parameter to pass in, so that th
);
}
}
In fact, there's nothing much different from using object objects in a method.
What's more, after JDK1.5 added the automatic unboxing function, eliminating the need for downward transition of trouble
Instance 4: Generic interface
Copy Code code as follows:
Definition of a generic interface
Interface inter{
public void print
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.