Java generics (generics) __java

Source: Internet
Author: User
Tags email string

Java generics are a new feature introduced in JDK 5 that allows the use of type parameters when defining classes and interfaces. generic class

Generic classes are the generic forms we use most often, as follows:

/**
 * Tool Class
 * Created by Yuedong.li on 12/8/15.
 * * Public
class Toolsutil<t> {
    /**
     * if-else Replace
     * @param condition condition
     * @param T1 object 1
     * @par Am T2 Object 2
     * @return result
     *
    /Public T IfThenElse (boolean condition, T T1, T T2) {
        if (condition) {
            R Eturn t1;
        }
        Return T2
    }
}

A tool class that is currently used to replace If-else

The use of the method is also very simple

public class Main {public
    static void Main (string[] args) {
        double res = new toolsutil<double> (). IfThenElse (1 > 2, 1.0, 2.0);
        System.out.println (res);
    }

generic Method

With more and more requirements, tool class Toolsutil may need to add a lot of tool classes, but not other methods of parameter return value is T, we need to do granularity adjustment, this time is the generic method performance.

/**
 * Tool Class
 * Created by Yuedong.li on 12/8/15.
 * * Public
class Toolsutil {
    /**
     * if-else Replace
     * @param condition condition
     * @param T1 object 1
     * @param T2 Object 2< c9/>* @param <T> Type
     * @return results
    /public <T> T IfThenElse (boolean condition, T T1, T T2) {
  if (condition) {return
            T1;
        }
        return t2;
    }

    /**
     * Check email * * @param email * email *
     @return is a legal email
    /public static Boolean checkemail (String email) {
        Boolean result = false;
        Some code about checking email string return result
        ;
    }

The code above we use the generic method to separate the IfThenElse function, no longer must be specified when creating the Toolsutil class.

The use of the method is still relatively easy:

public class Main {public
    static void Main (string[] args) {
        double res = new Toolsutil (). IfThenElse (1 > 2, 1.0 , 2.0);
        System.out.println (res);
        Boolean checkres = new Toolsutil (). Checkemail ("app@codeboy.me");
        System.out.println (checkres);
    }

You can see that when you create a toolsutil, you don't need to specify the type anymore, and the code looks a lot fresher.

We often write the method of the tool class as static, of course the generic method is also possible:

/**
 * Tool Class
 * Created by Yuedong.li on 12/8/15.
 * * Public
class Toolsutil {
    /**
     * Check email * * @param email * email *
     @return is a legal email c11/>*/public
    Static Boolean checkemail (String email) {
        Boolean result = false;
        Some code about checking email string return result
        ;
    }


    /**
     * If-else Replacement
     * @param condition condition
     * @param T1 object 1
     * @param t2 object 2
     * @param <T> type
     * @return Results
     *
    /public static <T> T IfThenElse (boolean condition, T T1, T T2) {
        if (condition) {
  
   return t1;
        }
        Return T2
    }
}

  

Use more concise, the operation is as follows:

public class Main {public
    static void Main (string[] args) {
        double res = toolsutil.ifthenelse (1 > 2, 1.0, 2.0) ;
        System.out.println (res);
        Boolean checkres = Toolsutil.checkemail ("app@codeboy.me");
        System.out.println (checkres);
    }

The point to note is that <T> must summarize between modifiers (public private static final, etc.) and return values

When is the generic method used? ① type constraints are only partially

Generics function not for the global, but only for the local, such as the Toolsutil class ② static methods need to deal with generics

As the ifthenelse in the example above, ordinary static methods cannot use generics. more articles please visit Xiao Fat Xuan

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.