Java base generic static method __java

Source: Internet
Author: User
Package pkg;
 /** * Created by Sanmao on 2016/6/29. * * public class Static {/** * from the previous blog with restricted wildcard characters, we see that the Totalarea method is a generic method because it can receive different parameters * But there is no specific type of parameter table, as Generic
     In the declaration of the Meorycell class, sometimes a particular type is important. * This may be because of the following reasons * 1. This particular type is used as the return type * 2. This type is for declaring a local variable * 3. This type acts on an extra type parameter * If so, you must declare a display generic method with several types of parameters * as the following code shows a generic static method that makes a series of lookups of value X in array arr by using a generic method * instead of using object as a non-pan method, we can get compile-time error when looking up an Apple object in an array of objects * Generic methods are particularly like generic classes, because type parameter tables can use the same syntax, and before the type parameters in the generic method are in the return type * @param args/P    Ublic static void Main (string[] args) {//static s=new static ();
        Non-static methods need to invoke object leixing lx1=new leixing (1,2);
        Leixing lx2=new leixing (3,4);
        Leixing[] Lx=new leixing[]{lx1,lx2};
        System.out.println (Static.contains (LX,LX2)); System.out.println (S.contains (LX, lx2)); Non-static methods need to invoke object} public static <AnyType> Boolean contains (anytype[] Arr,anytype x) {//public &L T Anytype> Boolean contains (anytype[] Arr,anytype x) {//Declare non-static object int z=0;
            for (AnyType Val:arr) {z++;
                if (X.equals (val)) {System.out.println (z);
            return true;
    return false;
    }//So write leixing is not an internal class, the inner class is not in the public class Main method of the new Leixing (), The outer class can class leixing{int x;
    int y;
    Public leixing () {};
        Public leixing (int x,int y) {this.x=x;
    This.y=y;
    public int GetX () {return x;
    public int GetY () {return y; }

}

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.