17th Zhang Yi Type and generics

Source: Internet
Author: User

1. Enumeration type

Enumeration types and generics are new in JDK1.5.

An enumeration type can override the way a previous constant is defined, wrapping a constant in a class or interface, and also provides a security check feature.

Enum types exist in nature or in the form of classes.

An enum is a keyword that defines an enumeration type.

For example: Create a constants interface in a project, and define the usual way to do it in an interface:

 Public Interface constants{    publicstaticfinalint constants_a=1;      Public Static Final int Constants_b=12;}

The addition of enumeration types gradually replaces the way the constants are defined, and the syntax for defining constants using enumeration types is as follows:

 Public enum constants{    constants_a,    constants_b,    Constants_c}

2. Generic type

(1) Generics are essentially types that enable programmers to define security.

In the absence of generics, Java also provides a reference to the object "arbitrary" operation, which is the object reference "down" and "Move Up" operations, but some forced type conversion errors may not be caught by the compiler, and after running an exception, There are security implications for forced type conversions, so a generic mechanism is provided again.

For example, create a test class in your project that transforms the base type up to the object type in the class:

 Public classTest {Private Objectb//defining an Object type member variable         Public ObjectGetb () {//set the appropriate getxxx () method        returnb; }         Public voidSETB (Object b) {//set the appropriate setxxx () method         This. B =b; }         Public Static voidMain (string[] args) {Test T=NewTest (); T.setb (  New Boolean (true));//up-conversion operationsSystem.out.println (T.getb ()); T.SETB (NewFloat (12.3));
Float F = (Float) (T.getb ());//down-conversion operationsSystem.out.println (f); }}

In this example, a private member variable B is defined in the test class, which is of type object and has a corresponding setxxx () and GetXXX () method defined for it.

In the main method of the class, the new Boolean (True) object is used as the parameter of the Setb () method, because the parameter type of the SETB () method is Object, which enables the "upward transformation" operation;

At the same time, when calling the Getb () method, the object returned by the Getb () method is returned with the appropriate type (this is float) , which is the "down transition" operation.

Because the "Move Up" is safe, the classcastexception exception occurs if you use the wrong type for a "down transition" operation, or if you do not perform the operation.

(2) Defining generic classes

The syntax for a generic mechanism is as follows:

Class name < public class Overclass<t> {//Set private T over; // defining generic member variables

         PublicT Getover () {//set the appropriate getxxx () method        returnb; }         Public voidSetover (T over) {//set the appropriate setxxx () method         This. B =b; }         Public Static voidMain (string[] args) {//instantiation of a Boolean object
Overclass<boolean> over1=new overclass<boolean> ();
Instantiate a float type Object
Overclass<float> over2=new overclass<float> ();

Over1.setover (True); No type conversion required Over2.setover (12.3f);

Boolean b=over1.getover (); No type conversions Required
Float F=over2.getover ();
System.out.println (b); System.out.println (f); }}

17th Zhang Yi Type and 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.