Zookeeper
1. Use a class with generic information:
A. Both sides are generic and must be consistent
ArrayList <String> list = new ArrayList <String> ();
B. It is also possible for a single side to have generic information
ArrayList <String> list = new ArrayList (); new programmers call the content of old programmers
ArrayList list = new ArrayList <String> (); old programmers call the content of new programmers
2. Custom generic:
A. You can declare the generic type after the defined class, And the instance method in the class can be directly used.
Public class Demo1 <T> {// generic definition at the class level. The "instance Method" in the class can be directly used
// <T>: Declaration of generic definition. Before the return value
Public T findOne (Class <T> clazz ){
Return null;
}
B. Static methods must be defined separately before they can be used. The return value is defined above.
Public static <T> void u1 (T t ){
}
C. Multiple generic definitions can be defined simultaneously.
Public static <K, V> V findValue (K k) {// MAP
Return null;
}
3. Considerations for using classes with generic information:
Only the object type can be used as the actual parameter of the generic method.
The dao interface remains unchanged.
The main difference is that the implementation has changed, and there is no need to write so much code.Reflection on Generics.
Import java. io. serializable; import java. lang. reflect. parameterizedType; import java. lang. reflect. type; import org. hibernate. session; import com. itheima. dao. customerDao; import com. itheima. dao. dao; import com. itheima. domain. customer; // use Hibernatepublic class BaseDao <T> implements Dao <T> {private Session session Session = null; // use this object as private Class clazz; // from which table to operate public BaseDao () {// generic reflection: Objective: assign a value to the member variable clazz so that the query and deletion can know from which table to operate System. out. println (this. getClass (). getName (); // this: the actual object CustomerDao cDao = new CustomerDaoImpl (); CustoemrDaoImpl instance Type = this. getClass (). getGenericSuperclass (); // obtain the parent Class with generic information "BaseDao <Customer>" Type is the Class interface ParameterizedType pType = (ParameterizedType) type; clazz = (Class) pType. getActualTypeArguments () [0];} public void add (T t) {session. save (t);} public void update (T t) {session. update (t);} public void delete (Serializable id) {T t = findOne (id); session. delete (t);} public T findOne (Serializable id) {System. out. println (clazz. getName (); return (T) session. get (clazz, id );}}
package com.jxnu.dao.impl;import java.util.List;import com.itheima.dao.CustomerDao;import com.itheima.domain.Customer;public class CustomerDaoImpl extends BaseDao<Customer> implements CustomerDao {//public CustomerDaoImpl(){//super();//}public List<Customer> findRecords(int startIndex, int pageSize) {// TODO Auto-generated method stubreturn null;}}
package com.jxnu.dao.impl;import java.util.List;import com.itheima.dao.UserDao;import com.itheima.domain.User;public class UserDaoImpl extends BaseDao<User> implements UserDao {@Overridepublic List<User> findAll() {// TODO Auto-generated method stubreturn null;}}
The implementation layer reduces code.
The key code is:
Private Class clazz; // from which table to operate
Public BaseDao (){
// Generic reflection: The purpose is to assign a value to the member variable clazz so that the query and deletion can know from which table the operation is performed.
System. out. println (this. getClass (). getName ());
// This: the real object CustomerDao cDao = new CustomerDaoImpl (); CustoemrDaoImpl instance
Type type = this. getClass (). getGenericSuperclass (); // obtain the parent Class with generic information. "BaseDao <Customer>" Type is the Class interface.
ParameterizedType pType = (ParameterizedType) type;
Clazz = (Class) pType. getActualTypeArguments () [0];
}