1. 程式人生 > >java中覆蓋(重寫)equals方法

java中覆蓋(重寫)equals方法

package com.forming.sapinterface;

import sun.java2d.pipe.SpanClipRenderer;

import java.util.Objects;

public class Sap {
private Integer rsnum;
private Integer rspos;
private Integer aufnr;
private Integer vornr;
private String lgort;
private Integer charg;
private Double bdmngt;
private String wertks;
private Integer matnr;
private String pargb;
public Sap{

}

public Sap(Integer rsnum, Integer rspos, Integer aufnr, Integer vornr, String lgort, Integer charg, Double bdmngt, String wertks, Integer matnr, String pargb) {
         this.rsnum = rsnum;
         this.rspos = rspos;
         this.aufnr = aufnr;
         this.vornr = vornr;
         this.lgort = lgort;
         this.charg = charg;
         this.bdmngt = bdmngt;
         this.wertks = wertks;
         this.matnr = matnr;
         this.pargb = pargb;
}
public Integer getRsnum() {
    return rsnum;
}
public void setRsnum(Integer rsnum) {
    this.rsnum = rsnum;
}
public Integer getRspos(){
    return rspos;
}
public void setRspos(Integer rspos){
    this.rspos = rspos;
}
public Integer getAufnr(){
    return aufnr;
}
public void setAufnr(Integer aufnr){
    this.aufnr = aufnr;
}
public Integer getVornr(){
    return vornr;
}
public void setVornr(Integer vornr){
    this.vornr = vornr;
}
public String getLgort(){
    return lgort;
}
public void setLgort(String lgort){
    this.lgort = lgort;
}
public Integer getCharg(){
    return charg;
}
public void setCharg(Integer charg){\
    this.charg = charg;
}
public Double getBdmngt(){
    return bdmngt;
}
public void setBdmngt(Double bdmngt){
    this.bdmngt = bdmngt;
}
public String getWertks(){
    return wertks;
}
public void  setWertks(String wertks){
    this.wertks = wertks;
}
public Integer getMatnr(){
    return matnr;
}
public void setMatnr(Integer matnr){
    this.matnr = matnr;
}
public String getPargb(){
    return pargb;
}
public void setPargb(String pargb){
    this.pargb = pargb;
}

重寫equals方法只需要5步,自己總結:
@Override
public boolean equals(Object o){
if(o == this){
return true;
}
if (o == null){
return false;
}
if (!(o instanceof Sap)){
return false;
}
Sap sap = (Sap) o;
if (sap.rsnum == this.rsnum && sap.rspos == this.rspos && sap.aufnr == this.aufnr && sap.vornr == this.vornr &&
sap.lgort.equals(lgort) && sap.charg == this.charg && sap.bdmngt == this.bdmngt && sap.wertks.equals(wertks)
&& sap.matnr == this.matnr && sap.pargb == this.pargb
){
return true;
}
return false;
}
這種跟軟體自動生成的功能一樣。