下面进行克隆,克隆是克隆对象,根据方法选择克隆的对象
@Override public Apply huixian(String aid) { Connection conn = BaseDao.getConn(); Apply ap=null; try { PreparedStatement ps = conn.prepareStatement("select apply.*,cname from apply,category where apply.cid=category.cid AND aid="+aid); System.out.println(ps.toString()); ResultSet rs = ps.executeQuery(); if (rs.next()){ ap=new Apply(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getDouble(5), rs.getString(6), rs.getInt(7), rs.getString(8), rs.getInt(9), rs.getString(10)); } } catch (SQLException e) { throw new RuntimeException(e); } return ap; }
以上是回显方法,我用到了Apply对象,所以我需要在Apply对象中实现克隆方法,下面是我的Apply中的内容
package com.bw.entity; import java.io.Serializable; /** * 列表变量 */ public class Apply implements Cloneable, Serializable { private int aid; private String name; private String idcrad; private String tel; private double amount; private String aoolydate; private int status; private String detail; private int cid; private String cname; public int getAid() { return aid; } public void setAid(int aid) { this.aid = aid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIdcrad() { return idcrad; } public void setIdcrad(String idcrad) { this.idcrad = idcrad; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public String getAoolydate() { return aoolydate; } public void setAoolydate(String aoolydate) { this.aoolydate = aoolydate; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Apply(int aid, String name, String idcrad, String tel, double amount, String aoolydate, int status, String detail, int cid, String cname) { this.aid = aid; this.name = name; this.idcrad = idcrad; this.tel = tel; this.amount = amount; this.aoolydate = aoolydate; this.status = status; this.detail = detail; this.cid = cid; this.cname = cname; } @Override public Object clone() throws CloneNotSupportedException { Apply apply=null; apply=(Apply) super.clone(); return apply; } }
鼠标放到Cloneable上,按alt加insert,选重写方法,选中克隆方法,下面的方法要用public,让test(测试类)调用,内容是我改过的,可以不动。
下面是我的测试类内容:
package com.bw.test; import com.bw.dao.ApplyDaoImpl; import com.bw.entity.Apply; public class Test { public static void main(String[] args) throws CloneNotSupportedException { ApplyDaoImpl app=new ApplyDaoImpl(); Apply apply_one = app.huixian("15"); Apply apply_two = (Apply) apply_one.clone(); System.out.println(apply_one==apply_two); System.out.println(apply_one.getAid()==apply_one.getAid()); } }
调用实现类的回显方法,然后对回显出来的对象进行克隆,然后比较,第一个比较的是对象,对象是不一样的,就像两辆一样的汽车,不可能是同一个,但对象里的数据是一致的。
下面是我的控制台效果