Java 5 generic usage, T. Class acquisition and cleaning Platform

Source: Internet
Author: User
Author: Jiangnan Baiyi

Too many books have been written in the generic Syntax of Java 5. GP must be useful. Otherwise, Java and C # will not be able to support GP at the same time. But it is also clear that the GP and ruby-style dynamic oo languages are different in ideology. If they are one vote, I think most of the civilian programmers are more keen on the plain white nature of dynamic oo languages. But if you don't want to switch to a dynamic language that supports jsr223, let's look at GP.

Summarize the four functions of generics randomly:
The first is generalization. You can use a t to represent any type. However, GP is forced out by the strict static nature of C ++, and falls into the plain of flowers like Java and C #-all objects are derived from objects except several primitive types, coupled with the reflection function of Java, the Java Collection library has never been as well as a normal model.

The second is generic + reflection, Because Java's generics cannot get t. class and I think generic is useless. I recently learned how to use the reflection API to obtain t class.

The third is convergence, which increases type security and reduces the code for forced type conversion. This is a weakness of Java Collection.

Fourth, you can build many things during the compilation period, such as metaprogramming. However, unless they can be completely enclosed in the framework, users and extensions of the framework do not need to learn the usage of these things. Otherwise, they are solely at the People's box office. C ++'s metaprogramming is amazing, but by comparing Python's simple Syntax of creating a class with meta programming, we can see what is truly a good call and a great call.

Therefore, as an architecture designer, we should use the second and second usage of the above, and use reflection and generics in the framework class to make the framework more powerful. At the same time, we adopt the convergence feature, in the spirit of being responsible to the people, the generic model is used to make the framework more secure and less forced type conversion.

The wiping method avoids Java's bleeding split:
We often scold Java GP for its wiping method, but I think thanks to its moderate feature-if you use it as a model, you don't need to use it as a common object, this prevents the Java camp from going through a split to be or not to be.
The biggest example is the collection framework of Java 5. For example, some students insist that they will not be an idiot for type errors, furthermore, it is hard to bear that each definition must contain a generic definition list <book> 〉 The code saved by no forced type conversion is not enough to define N points (by the way, there is no tyepdef... in Java .....), therefore, it is very difficult for the model to catch a cold. At this time, we would like to thank you for this dirty wipe method so that you can still maintain a Non-generic usage for a generic framework...

Obtain T. class through reflection:

I don't know why I don't know much about this in the book. I only know it when I know it. For the most typical application, see generic data access objects in hibernate wiki. The Code is as follows:

Abstract Public class basehibernateentitydao <t> extends hibernatedaosupport {
Private class <t> entityclass;
Public basehibernateentitydao (){
Entityclass = (class <t>) (parameterizedtype) getclass ()
. Getgenericsuperclass (). getactualtypearguments () [0];
}
Public t get (serializable ID ){
T o = (t) gethibernatetemplate (). Get (entityclass, ID );
}
}

The essence is this sentence: class <t> entityclass = (class <t>) (parameterizedtype) getclass (). getgenericsuperclass (). getactualtypearguments () [0];

After the wildcard type, all the sub-classes of basehibernateentitydao need to define the generic type, so they do not need to overload the getenttityclass (), get (), and find () functions. The sales benefit is quite obvious, therefore, the DaO base class of springside is generic without hesitation.

However, the wiping method is still large, so the generic Syntax of sub-classes cannot be written in disorder. The most correct usage is only:

Public class bookdao extends basehibernateentitydao <book>

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.