Talking about generics in my eyes and generics in my eyes

Source: Internet
Author: User

Talking about generics in my eyes and generics in my eyes

I came into contact with generics a long time ago, but I didn't know the benefits of generics at that time. I used generics in the abstract process and later came into contact with the gxpt system, the generic method is used in this system framework. at that time, I only knew that it could solve the problem of repeated code writing, but I did not understand it. Later I came into contact with the Itoo framework, which used generic classes, at the same time, I am involved in this system. Compared with gxpt, I have realized the benefits of using generics.

I. Introduction

Generics are new features of Java SE 1.5. The nature of generics is parameterized, that is, the Data Type operated is specified as a parameter. This type of parameter can be used to create classes, interfaces, and methods, which are called generic classes, generic interfaces, and generic methods.


2. Solve the Problem

When JDK does not have a generic type, you can use the Object type to implement "arbitrary" parameters ". explicit forced type conversion is required to achieve arbitrary parameters. in addition, this forced conversion requires developers to predict the actual parameter type. if the forced type conversion is incorrect, the compiler may not prompt an error and the exception occurs during running, which is a major security risk. if the generic type is used, this error can be found in the compilation phase, and all mandatory conversions are both automatic and implicit, which improves the code reuse rate again. to sum up one sentence, the generic model solves the problem of explicit forced conversion in the abstract process, turning the initiative into passive, and improving the code reuse rate again.

 

Iii. Instances

No generics. Use Object + forced conversion to implement Abstraction:

<Span style = "font-size: 18px;">/*** use the Object type, and the display must be forcibly converted * @ author huan **/public class NoGen {private Object ob; // define the generic member variable public NoGen (Object ob) {this. ob = ob;} public Object Getob () {return ob;} public void setOb (Object ob) {this. ob = ob;} public void showType () {System. out. println ("the actual type of T is:" + ob. getClass (). getName () ;}} public class GenDemo2 {public static void main (String [] args) {// define an Intege of the generic class NoGen R version NoGen Release B = new NoGen (new Integer (88); Release B. showType (); int I = (int) returns B. getob (); System. out. println ("Value =" + I); System. out. println ("------------------"); // defines a String version of the generic type Gen, NoGen strOb = new NoGen ("Hello Gen! "); StrOb. showType (); String s = (String) strOb. getob (); System. out. println ("value =" + s) ;}</span>

Corresponding class diagram:



ABSTRACT Using Generics:


<Span style = "font-size: 18px;"> public class Gen <T> {private T ob; // defines the generic member variable public Gen (T ob) {this. ob = ob;} public T Getob () {return ob;} public void setOb (T ob) {this. ob = ob;} public void showType () {System. out. println ("the actual type of T is:" + ob. getClass (). getName () ;}}/*** use wildcard * @ author huan **/public class GenDemo {public static void main (String [] args) {// define an Integer version of the generic type Gen <Integer> limit B = new Gen <Integer> (8 8); then B. showType (); int I = Hangzhou B. getob (); System. out. println ("Value =" + I); System. out. println ("------------------"); // defines a String version of generic Gen. Gen <String> strOb = new Gen <String> ("Hello Gen! "); StrOb. showType (); String s = strOb. Getob (); System. out. println (" value = "+ s) ;}</span>

Corresponding class diagram:


The two class diagrams show that the abstraction of Object + forced conversion is passive, and the abstraction of generic items is active. use active replacement to make the encapsulated code more invasive.


Iv. Differences between generic classes and generic methods

In terms of scope, the scope of generic classes is smaller than that of generic methods, and the restriction on the use of generic classes is greater.

According to the loading situation, the loading time of generic classes is delayed, which is a type of lazy loading.

 

V. Summary

1. If the generic type is used, whether the parameters in the generic type are URL-based or value-based.

Answer: It is the address value.

2. from the perspective of reusability, generic classes and generic methods are better. gxpt uses generic methods and itoo uses generic classes. itoo uses generic classes to further encapsulate the underlying layer. improve code reuse rate.

3. When using a set, generic type is used. Therefore, the container is a set + method. In a container, generic type is also commonly used. Therefore, when learning about containers, we should first understand the idea of generics.



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.