Simply speaking generics in Java, the use of DAO layers in SSH simplifies the amount of code

Source: Internet
Author: User

Originally just listen to the teacher said generics particularly useful, but then always make Android with the generic type is less, but it is really important to feel it, so spent an afternoon of time to write a demo, good, the old, the code:

First of all, Sysout is a tool, but the use of the wrong, bloggers in accordance with their own habits wrote a tool class,

S

Package cn.edu.sjzc.fanyafeng.testlamejni.util;/** * System Unified Management Class * * @author Shian Wind */public class S {private S () {    /* Cannot be instantiated */throw new Unsupportedoperationexception ("cannot be instantiated"); public static Boolean IsSystem = true;//If you need to print a bug, you can initialize the private static final String TAG inside the OnCreate function of the application = "---|    Sysout output: ";        public static void L () {if (IsSystem) {System.out.println ("--------------------------------------"); }}//The following two are the default SYS functions public static void P (Object msg) {if (IsSystem) {System.out.print (        TAG + msg + "|---");        }} public static void P (String msg) {if (IsSystem) {System.out.print (TAG + msg + "|---");        }} public static void P (int msg) {if (IsSystem) {System.out.print (TAG + msg + "|---"); }} public static void pl (Object msg) {if (IsSystem) {System.out.println (TAG +msg + "|---");        }} public static void pl (String msg) {if (IsSystem) {System.out.println (TAG + msg + "|---"); }} public static void pl (int msg) {if (IsSystem) {System.out.println (TAG + msg + "|---")        ; }}//The following is the function that passed the custom tag public static void P (Object tag, Object msg) {if (IsSystem) {System.out        . Print ("---|" + tag + ":" + msg + "|---"); }} public static void P (string tag, string msg) {if (IsSystem) {System.out.print ("---|" + tag        + ":" + msg + "|---"); }} public static void P (String tag, int msg) {if (IsSystem) {System.out.print ("---|" + tag + "        : "+ msg +" |---"); }} public static void Pl (Object tag, Object msg) {if (IsSystem) {System.out.println ("---|" + t        Ag + ":" + msg + "|---");           }} public static void pl (string tag, string msg) {if (IsSystem) { SYSTEM.OUT.PRINTLN ("---|" + tag + ":" + msg + "|---"); }} public static void pl (String tag, int msg) {if (IsSystem) {System.out.println ("---|" + tag        + ":" + msg + "|---"); }    }}
This does not have any technical content, I believe everybody hug one eye to know what meaning, good, come to see Bo definition of two interfaces:

One of the parameters:

Package cn.edu.sjzc.fanyafeng.testlamejni.test;/** * Created by Administrator on 2015/6/9/0009. */public interface genericsinterfaceoneparams<t> {    void show (T t);}
Two parameters:

Package cn.edu.sjzc.fanyafeng.testlamejni.test;/** * Created by Administrator on 2015/6/9/0009. */public interface Genericsinterfacetwoparams<t, u> {    void show (T T, u u);}

Take a look at the implementation of a parameter:

The first one is the int type

Package Cn.edu.sjzc.fanyafeng.testlamejni.test;import cn.edu.sjzc.fanyafeng.testlamejni.util.s;/** * Created by Administrator on 2015/6/9/0009. */public class Oneintparams implements genericsinterfaceoneparams<integer> {    @Override public    void Show (Integer integer) {        s.pl (integer);    }}
The second one is the list type

Package Cn.edu.sjzc.fanyafeng.testlamejni.test;import Java.util.arraylist;import Java.util.iterator;import Java.util.list;import cn.edu.sjzc.fanyafeng.testlamejni.util.s;/** * Created by Administrator on 2015/6/9/0009. */public class Onelistparams implements genericsinterfaceoneparams<list> {    @Override public    void Show ( List arrayList) {for        (Object item:arraylist) {            s.pl (item);        }        S.L ();        Iterator Iterator = Arraylist.iterator ();        while (Iterator.hasnext ()) {            s.pl (Iterator.next ());}}}    
An iterator and an enhanced for loop are used here

The third is a string of type

Package Cn.edu.sjzc.fanyafeng.testlamejni.test;import cn.edu.sjzc.fanyafeng.testlamejni.util.s;/** * Created by Administrator on 2015/6/9/0009. */public class Onestringparams implements genericsinterfaceoneparams<string> {    @Override public    Void Show (String s) {        s.pl (s);    }}
Take a look at the string implementation of two parameters

Package Cn.edu.sjzc.fanyafeng.testlamejni.test;import cn.edu.sjzc.fanyafeng.testlamejni.util.s;/** * Created by Administrator on 2015/6/9/0009. */public class Twostringparams implements Genericsinterfacetwoparams<string, string> {    @Override    public void Show (string s, String t) {        s.pl (s, t);    }}

And look at any type, here's an example.

Package Cn.edu.sjzc.fanyafeng.testlamejni.test;import cn.edu.sjzc.fanyafeng.testlamejni.util.s;/** * Created by Administrator on 2015/6/9/0009. */public class Twoparams implements Genericsinterfacetwoparams<string, integer> {    @Override public    Void Show (String s, integer integer) {        s.pl (s, integer);}    }

There is also a test type of

Package Cn.edu.sjzc.fanyafeng.testlamejni.test;import cn.edu.sjzc.fanyafeng.testlamejni.util.s;/** * Created by Fanyafeng on 2015/6/9/0009. */public class Generics<t> {    private T t;    Public generics (T t) {        this.t = t;    }    Public T Gett () {        return t;    }    public void sett (T t) {        this.t = t;    }    public void ShowType () {        s.pl ("The actual type of" T is: ", T.getclass (). GetName ());}    }
The final test is the main entrance.

Package Cn.edu.sjzc.fanyafeng.testlamejni.test;import Java.util.arraylist;import Java.util.list;import cn.edu.sjzc.fanyafeng.testlamejni.util.s;/** * Created by Administrator on 2015/6/9/0009.        */public class Genericstest {public static void main (string[] args) {s.pl ("Test generics");        S.L ();    Init ();        } private static void Init () {Initinteger ();        Initstring ();        Initonestringparams ();        Initoneintparams ();        Inittwostringparams ();        Inittwoparams ();        Initobject ();        Initarray ();    IsBlank ("Gender", "male", "csdn", "http://blog.csdn.net/qq_23195583"); } private static void Initinteger () {generics<integer> integergenerics = new Generics<integer> (518)        ;        Integergenerics.showtype ();        int i = Integergenerics.gett ();        s.pl ("value=", I);    S.L (); } private static void Initstring () {generics<string> stringgenerics = new Generics<string> ("Fanyafe        Ng "); StRinggenerics.showtype ();        String s = Stringgenerics.gett ();        s.pl ("value=", s);    S.L ();        } private static void Initonestringparams () {onestringparams onestringparams = new Onestringparams ();        Onestringparams.show ("Fanyafeng");    S.L ();        } private static void Initoneintparams () {oneintparams oneintparams = new Oneintparams ();        Oneintparams.show (24);    S.L ();        } private static void Inittwostringparams () {twostringparams twostringparams = new Twostringparams ();        Twostringparams.show ("name", "Shian Wind");    S.L ();        } private static void Inittwoparams () {twoparams twoparams = new Twoparams ();        Twoparams.show ("Age", 24);    S.L (); } private static void Initobject () {//blogger This does not seem to have a generic type, a bit of a mask fanyafengbean Fanyafengbean = new Fanyafengbean ("        Shian Wind ");        Fanyafengbean.show ("name");        Fanyafengbean fanyafengBean1 = new Fanyafengbean (24);    Fanyafengbean1.show ("Age");    S.L ();        } private static void Initarray () {onelistparams onelistparams = new Onelistparams ();        List List = new ArrayList ();        List.add ("name");        List.add ("Shian Wind");        List.add ("Age");        List.add (24);        Onelistparams.show (list);    S.L ();        public static void IsBlank (String ... str) {if (str = = NULL | | str.length = = 0) {return;            } else {for (String s:str) {s.pl (s);        }} S.L ();    Return }}//public static Boolean IsBlank (String ... str) {//if (str = = NULL | | str.length = = 0) {//return                    true;//} else {//for (String s:str) {//if (Genericstest.isblank (s)) {// s.pl (s);//return true;//}//}//return false;//}/ /    }//}

Would like to write an arbitrary number of parameters of the interface, but Bo master ability is not enough, only write a specific method of implementation, Bo Master to delve into their own, finally a









Simply speaking generics in Java, the use of DAO layers in SSH simplifies the amount of code

Related Article

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.