Talk about the generics in my eyes

Source: Internet
Author: User

I was exposed to generics early on, but at that time I did not know what the benefits of generics were, using generics in the process of abstraction, and then contacting the GXPT system, which was used in a generic way. At that time, I just knew that it would solve the problem of repeating code, but I didn't realize that I was exposed to the Itoo framework, which used generic classes, and I was participating in the system, and I realized the benefits of using generics compared to GXPT.

I. Introduction

Generics are a new feature of Java SE 1.5, and the nature of generics is a parameterized type, meaning that the data type being manipulated is specified as a parameter. This type of parameter can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively.


two. Problem solving

When the JDK1.5 is not yet generic, it implements the "arbitrariness" of the arguments by using the type Object. Implementing parameter arbitrariness requires explicit coercion of type conversions. And this coercion requires the developer to be predictable about the actual parameter type. For cases where a type conversion error is enforced, The compiler may not prompt the error, when running the exception, this is a great security risk. If you use generics, you can find this error at compile time, and all casts are automatic and implicit. Once again, the reuse rate of the code is improved. Summing up a sentence is the generic solution to the abstract process requires explicit casting, the initiative becomes passive, and again improve the code reuse rate.

three. Example

No generics, using object+ casts to implement abstractions:

<span style= "FONT-SIZE:18PX;" >/** * Using the object type, display requires cast * @author Huan * */public class Nogen {private Object ob;//define Generic member variable public nogen (Object ob) {This.ob=ob;} Public Object Getob () {return OB;} public void Setob (Object ob) {this.ob=ob;} The actual type of public void ShowType () {System.out.println ("T" is: "+ob.getclass (). GetName ());}} public class GenDemo2 {public static void main (string[] args) {//defines an Integer version of the generic class Nogen Nogen intob=new nogen (New Integer ( ); Intob.showtype (); int i= (int) Intob.getob (); System.out.println ("value=" +i); System.out.println ("------------------");//define a string version of generic class Gen Nogen strob=new Nogen ("Hello gen!"); Strob.showtype (); String s= (String) Strob.getob (); System.out.println ("value=" +s);} </span>

Corresponding class diagram:



Use generics to implement abstractions:


<span style= "FONT-SIZE:18PX;" >public class Gen<t> {private T ob;//define Generic member variable public Gen (T ob) {this.ob=ob;} Public T Getob () {return OB;} public void Setob (T ob) {this.ob=ob;} The actual type of public void ShowType () {System.out.println ("T" is: "+ob.getclass (). GetName ());}} /** * Using generics * @author Huan * */public class Gendemo {public static void main (string[] args) {//define an integer version of generic class Gen gen< integer> intob=new gen<integer> (n); Intob.showtype (); int I=intob.getob (); System.out.println ("value=" +i); System.out.println ("------------------");//define a String version of generic class Gen gen<string> strob=new gen<string> (" Hello gen! "); Strob.showtype (); String S=strob.getob (); System.out.println ("value=" +s);} </span>

Corresponding class diagram:


As you can see from the two class diagrams, the abstraction of the object+ coercion implementation is passive, and the abstraction to use generic matters is active. Use active substitution passive to make the encapsulated code less intrusive.


four. The difference between a generic class and a generic method

From the scope, the scope of the generic class is smaller than the generic method. The limitations of using generic classes are larger.

From the loading situation, the generic class deferred load time, which belongs to lazy loading.

Five. Summary

1. Using generics, whether the parameters in the generic are address-passing or by-value.

Answer: Is the address pass value

2. From the perspective of reusability, the generic and generic methods are better. GXPT is using a generic method that itoo the generic class used. Itoo uses generic classes to further encapsulate the underlying. Increase the code reuse rate.

3. When using a set, the generic is used, then the container is a collection + method, and in the container, generics are commonly used. So when learning containers, you should first understand the idea of generics.



Talk about the generics in my eyes

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.