A preliminary study of Java generics

Source: Internet
Author: User
Tags class generator generator

1. The understanding of the generic type

A high-level use of Java is the generic class, many basic frameworks will be built with generics, such as Gson ... Generics is really a good thing, but it is a sea of hundreds of rivers ... So as an Android developer it's more necessary to master this stuff.

Generics can be designed to return different types for a shared feature

Generics can be said to be a kind of constraint, can also be said to be a type of accommodation, extension ... I'm not saying it, it's purely personal understanding.

Constraint: Refers to the generic type if there is a specific type, then this is a constraint on the generic class, only to this specific type of work, ignoring all classes

Extension: The generic type that refers to is not a specific class type, but T, so it works for all class types.

2. The distinction of generic interfaces, generic classes, and generic methods

1, generic interface: In fact, and the normal interface is no different, is more than a generic, can be designed as a multi-purpose template interface, declared as follows:

/** *
 class Generator
 * @author ZHONGWR
 * *
 @param <T> * * Public
interface Generator<t> { Public
	T Next ();
}

2, generic class: And ordinary classes are no different, that is, more than a generic, can be designed for multi-purpose, a bit like a combination of classes: the declaration is as follows:

/** *
 Class Builder
 * * 
 @author ZHONGWR * * @param <T> * * Public
class basicgeneratorimp< T> implements generator<t> {
	private class<t> type;

	Public Basicgeneratorimp (class<t> type) {
		this.type = type;
	}
}

This is a generic class declaration that receives a class. class type.

3, generic methods: Needless to say, are routines, and the use of common methods is no different, is more than one generic parameter, the generic return value, so when the argument must give a definite type, otherwise the generic method has no meaning;

/**
	 * Generic method, used to create a class: A generic method is independent of a class and does not matter with the generic class entity class 
	 * ZHONGWR PM 9:08:13 
	 * generator<t> *
	public static <T> generator<t> Create (class<t> type) {return
		new basicgeneratorimp<t> ( type);
	

Universal generic method, so if you can, try to use the sea-na-Chuan generic method.

Next, use the Java programming idea: Finally, the demo of Java Engineering

Generic interface

/** *
 class Generator
 * @author ZHONGWR
 * *
 @param <T> * * Public
interface Generator<t> { Public
	T Next ();
}

/** *
 Class Builder
 * * 
 @author ZHONGWR
 * *
 @param <T>
* * public class Basicgeneratorimp<t> implements generator<t> {
	private class<t> type;

	Public Basicgeneratorimp (class<t> type) {
		this.type = type;
	}

	Public T Next () {
		try {return
			type.newinstance ();
		} catch (Exception e) {
			e.printstacktrace ();
			throw new RuntimeException ();
		}

	/**
	 * Generic method, used to create a class: A generic method is independent of a class and does not matter with the generic class entity class 
	 * ZHONGWR PM 9:08:13 
	 * generator<t> *
	public static <T> generator<t> Create (class<t> type) {return
		new basicgeneratorimp<t> ( type);
	}


Package type.coffee;

/** *
 Making Coffee class * * 
 @author Administrator */public
abstract class Coffee {
	private static int counter = 0;
	Private final Long id = counter++;

	Public String toString () {return
		getclass (). Getsimplename () + "" + ID;
	}
}


Import Type.coffee.Americano;
Import Type.coffee.Coffee;

/** *
 Test entrance * * 
 @author ZHONGWR * * @param <T> * * Public
class Maintest { Public
	static void Main (string[] args) {
		
		//Use generic methods to create generic class
		System.out.println ("className =" + Basicgeneratorimp.create (Americano.class). Next ());
	}



demo:http://download.csdn.net/detail/zhongwn/9651107

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.