Java language with generics

xiaoxiao2021-03-06  44

Introduction to Generics Generics is a technology in programming language, refers to parameterizing the data type in the program. It is essentially an abstraction, expansion of the data type of the program, and expands the expression of the language while supporting a larger granular code. use. For some types of data type parameters, they often have better readability, reuse and reliability. When designing a collection class and their abstract operation, it is often necessary to define them as unrelated to the specific data type. In this case, using generics is very suitable. For example, if we need to design a Stack class, sometimes the elements are stack, sometimes the elements may require the element of the Boolean or Object type. If you do not use generics, we usually need to define different multiple classes, or through inheritance. By inheritance, it is often analyzed later in this article. If you use the generics technology, you will use the Stack's element type, then the STACK class only needs to implement a version. When you need a STACK, you can create a Stack object as a parameter. ENEERICS and Java Language Generics Technology have been raised very early, but Generics's implementation and promotion is not smooth. This is because Generics has a large revision of language specification, and is also high in compilation technology, in addition, in the 1990s, all languages ​​actively expand object-oriented technology, and did not pay too much attention to Generics. So far, only a small number of languages ​​support generics, such as ADA, C , Eiffel, etc. In C , Template is used to realize Generics, and C also provides standard template libraries (STL, Standard Template Library) for developers, which greatly improves programming, so Generics is often mixed with template. However, until JDK1.4.1, Java language does not support generics. At present, the parameterization of the data type is not capable of direct implementation, but is usually indirectly implemented inherited. Due to Java, all classes are Object subclasses, so we can define the type as Object if you need to support multiple types, and can be implemented by the parent class and subcaters when used. Take the stack above as an example, we can define the type of Stack as the object type, then all Java objects can be placed in this Stack. When accessing objects, we can force objects to convert to the required type. However, this method will also bring some problems. When compiling, the type check is relaxed. Many problems can only be found to achieve complex type parameters, lack of expression capability, increase the trouble of the user, and perform type inspection due to Java distinction Object and basic types, so this approach is not easy to support basic types. Although the current Java language specification does not support generics, some organizations and individuals implement Generics by extending Java languages, which are more famous with GJ, Polyj and Nextgen. Java Language Standard Development Organization Java Community Process (JCP) also has previously received recommendations about supporting generics in the Java language, and has been discussing whether to support generics in Java language.

Among them, a more important milestone is a proposal proposed by Gilad Bracha, etc. 2001. GENERICS has a big impact on the Java language itself, the Class file format and JVM structure have a large impact; in addition, there are some problems in the implementation in C , which should be avoided in Java, and finally, there are some people. It is believed that Generics's introduction will lose the simplicity of the Java language. Based on these considerations, the current Java language is still unable to support generics, but according to some relevant personnel expects, the end of 2003, JDK1.5 is likely to support generics. In this chapter in this chapter, we will preview some Java's generics usage, although these technologies are not officially launched, but according to the draft version proposed by Java Generics, these usage is relatively stable and mature, expected They will not have much difference with the formal specification. In addition, these generics programs can be converted into compatible Java class files through a dedicated compiler, and can be run in the previous JRE environment, ensuring forward compatibility. Some of this chapter comes from the draft version proposed by Java language generics, and some other examples also refer to GJ's operational results. 1) Generals definition: In order to use generics, you must first define a class, interface, or method that supports generics, which is similar to the syntax of the C language template. <> Used to include the parameterization type, the parameterization type is indicated by Java identifier, usually using uppercase letters, such as T, A, B, etc. 1.1) Class and Interface Definition The following is the simplest generics class definition, define a parameterized type T1: interface mylist {... ..} The following is an interface to support multiple parameterized types: Interface MYLIST {... ..} Java supports the type of parameterization with restrictions, which means that the actual type must meet the restriction conditions when constructing the object. In the following example, T1 must implement class Comparable interface, T2 type must be subclass of the Component class, otherwise the construct fails. These limitations are usually performed when compiling. Interface MYLIST {} Complex definition can have a limited declaration, and even use forward reference. Class Test Void SWAP (ELEM [] A, INT I, INT J) ELEM TEMP = a [i]; a [i] = a [j] a [j] = TEMP;} Depending on the defined generics method example.

Public static Void SWAP (ELEM [] A, INT I, INT J) ELEM TEMP = a [i]; a [i] = a [j] a [j] = temp;}> 1.3) actual Examples One but java language supports generics, most of Collection will rewrite in generics mode, in fact, these classes have been rewritten in some Java extensions that support Gnerics. Now let's give a GJ, use the definition of the HashTable rewritten by generics. Public Class HashTable EXTENDS DICTIONY Implements Map , java.lang.cloneable, java.io.serializable {public v PUT (K Key, V Value) {...} ... ....} Different from previous HashTable definitions, it increases two types for expressing Key and Value parameterization (k, v), because Dictionary and MAP will support generics, so they use generics's expression. 2) GENERICS: 2.1) Creating an object generics class Before use, you must initialize the definition, set the actual type of parameterization type, the following is some examples of constructing HashTable and Vector. Hashtable ht1 = new hashtable (); hashtable ht2 = null; vector v1 = new vector (); vector v2 = new vector (); 2.2) Generics object class In the previous example, for V1 and V2 objects, they are objects of the Vector class, they have the same Class type, in other words, when running, the following expressions Truth. Although they are created using different parameter types, their Class types are the same. ASSERT (v1.getclass (). Equals (v2.getclass ()))); however, if we define a method with a Vector parameter, incoming a vector object when calling the method, This will cause compilation to fail. This is because the two objects are treated as different types when compiling. Void method (Vector V) {}; ... Vector v2 = new vector (); method (v2); // Compile failed 2.3) Type Check When constructing a generics object, the compiler will First check if the parameterization type is valid, such as satisfying the restriction condition, etc., determines all parameterized types, and then the compiler will check the type check, if you conform to the definition, then compile, otherwise you will compile, Report type check error. Therefore, in this way, the compiler can check the many types of miscible errors, avoiding the developer's errors, which is also one of the important advantages of Generics.

The following is some examples of type check: Vector strVector = new vector (); vector intVector = new vector (); strVector.add (new string ()); // OkstrVector. Add (new object ()); // Compile failed, requires String class strVector.add (new integer ()); // Compile failure, requires String class intVector.add (new integer ()); // okintvector.add ( New string ()); // Compile failure, the integer class 2.4) The mandatory conversion of classes is the principle of the inteenition class, and we can use the universal conversion rules. It should be noted that the objects of different parameterized types defined by the same generics class cannot be converted. For example, VECTOR and Vector are unconverted. In addition, Object can not be converted to the generics type, but the generics class can be converted to Object. The following is some examples of transformation class Dictionary extends object {} Class HashTable Extends Dictionary {} Dictionary D = New Dictionary () Hashtable h = new hashtable (); hashtable hfd = new hashtable (); object o = new object (); 1) d = ( Dictionary ) h // Compile success, run success; they have parent and child relationships. 2) h = (Hashtable ) D; // Compiles success, run failure; they have parent-child relationships. 3) h = (HashTable ) O; // Compiling Failed, Object cannot be converted to the generics class; 4) HFD = (HashTable ) d; // Compiling failed; four generics design and Implementation 1) Java Generics and C Template Since Java's Gnerics Design After C Template, Java's Generics desigbs a lot of experience and lessons of Template, especially Generics to avoid some questions known. Problems. First of all, different from template is that generics is required to perform type check, and Template does not provide this feature, which makes generics more secure. In addition, Java's Generics program only needs to be compiled. All programs can be multiplexed by all programs, and the Template implementation is to compile each to a new class using the template variable, which will cause a certain redundancy code. 2) GENERICS's implementation of Generics Java has many different implementations, more famous with GJ, Polyj and Nextgen.

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

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