收藏 java新手,关于this的用法不是太懂,?
public class Dept {
}
public class Emp {
}
//////////////////////////////////////////////////////////////////////
public interface IDeptDAO extends IGenericDAO<Dept>{
}
public interface IEmpDAO extends IGenericDAO<Emp> {
}
public interface IGenericDAO<T> {
void add(T newObj);
Long remove(Serializable id);
void update(Serializable id, T newObj);
T get(Serializable id);
List<T> getAll();
}
///////////////////////////////////////////////////////////////////////////////////////
public class DeptDAOImpl extends GenericDAOImpl<Dept> implements IDeptDAO {
public DeptDAOImpl(){
super();
}
}
public class EmpDAOImpl extends GenericDAOImpl<Emp> implements IEmpDAO {
}
abstract public class GenericDAOImpl<T> implements IGenericDAO<T> {
//GenericDAOImpl<T> 里面T的真正类型的字节码;
private Class<T> clz;
public GenericDAOImpl(){
//System.out.println(this.getClass().getSuperclass());
//System.out.println(this.getClass().getGenericSuperclass());
Type t = this.getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) t;
clz = (Class<T>) pt.getActualTypeArguments()[0];
System.out.println("--->" + clz);
}
public void add(T newObj) {
}
public Long remove(Serializable id) {
return null;
}
public void update(Serializable id, T newObj) {
}
public T get(Serializable id) {
try {
return clz.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public List<T> getAll() {
return null;
}
}
///////////////////////////////////////////////////////////////////////////////////
public class Test {
public static void main(String[] args) {
/**
* 创建子类对象之前,会先调用父类的构造方法
*/
IDeptDAO deptDao = new DeptDAOImpl();
Dept dept = deptDao.get(123);
System.out.println(dept);
IEmpDAO empDao = new EmpDAOImpl();
Emp emp = empDao.get(222);
System.out.println(emp);
// new GenericDAOImpl();
}
}
/*我不是太了解其中this指代的对象是什么,各位大神能否为我详细解释一下this的用法,以及super的用法,?谢谢麻烦给我了!
------解决方案--------------------说得好 理论东西看点文章比较好 这里的人回答你的会很乱 看的你也会很乱
------解决方案--------------------举个短一点的例子。比如下面这个类:
public class Person {
String name;
public Person(String name) {
this.name = name;
}
public void eat() {
System.out.println(this.name + " is eating.");
}