To put it simply, java generic, the dao layer in ssh will simplify the amount of code
In the past, I only heard the teacher say that generics are particularly useful, but I have been using fewer generics for android, but I feel that they are really important, so it took me one afternoon to write a demo. Well, the old rule is:
First, sysout is a tool, but it is difficult to use. The blogger writes a tool class according to his own habits,
S:
Package cn.edu. sjzc. fanyafeng. testlamejni. util;/*** System Unified management class ** @ author Fan Yafeng */public class S {private S () {/* cannot be instantiated */throw new UnsupportedOperationException ("cannot be instantiated");} public static boolean isSystem = true; // whether to print bugs, you can initialize private static final String TAG = "--- | sysout output:"; public static void l () {if (isSystem) {System. out. println ("------------------------------------") ;}// the following two default sys functions are 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 for passing in 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 ("--- |" + tag + ":" + 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 + "| ---");}}}
There is no technical content. I believe you will know what it means at a Glance. Well, let's look at the two interfaces defined by Bo:
For one parameter:
package cn.edu.sjzc.fanyafeng.testlamejni.test;/** * Created by Administrator on 2015/6/9/0009. */public interface GenericsInterfaceOneParams
{ void show(T t);}
Two parameters:
package cn.edu.sjzc.fanyafeng.testlamejni.test;/** * Created by Administrator on 2015/6/9/0009. */public interface GenericsInterfaceTwoParams
{ void show(T t, U u);}
Let's look at the implementation of a parameter:
The first one is of 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
{ @Override public void show(Integer integer) { S.pl(integer); }}
The second is 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
{ @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()); } }}
Here we use an iterator and an enhanced for loop.
The third is string 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
{ @Override public void show(String s) { S.pl(s); }}
Let's take a look at the String implementation of the 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
{ @Override public void show(String s, String t) { S.pl(s, t); }}
Let's take a look at any type. Here is 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
{ @Override public void show(String s, Integer integer) { S.pl(s, integer); }}
There is also a test Type
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
{Private 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 ());}}
Finally, the main entrance for testing
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 generic"); 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
IntegerGenerics = new Generics
(518); integerGenerics. showType (); int I = integerGenerics. getT (); S. pl ("value =", I); S. l ();} private static void initString () {Generics
StringGenerics = new Generics
("Fanyafeng"); 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", "Fan Yafeng"); S. l ();} private static void initTwoParams () {TwoParams twoParams = new TwoParams (); twoParams. show ("Age", 24); S. l ();} private static void initObject () {// The blogger does not seem to use generics. It's a bit confusing. FanyafengBean fanyafengBean = new FanyafengBean ("Fan Yafeng"); 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 ("Fan Yafeng"); 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 ;//}//}//}
I originally wanted to write an interface with any number of parameters, but the blogger was not capable enough and only wrote a specific implementation method. The blogger did his own research and finally came up with