比較兩個物件引數是否一致

2021-10-06 11:43:27 字數 1094 閱讀 7790

專案中需要將後台更新操作記錄到資料庫中,並且需要詳細記錄哪個字段發生改變

大概思路就是使用反射。先獲取到更新前後的引數,以更新後引數為比較依據獲取屬性名,再分別獲取更新前後的值進行比較

//比較兩個object型別引數是否一致

public

static string comparediff

(object afterupdateobject, object beforeupdateobject)

class<

?> afterclass = afterupdateobject.

getclass()

; field[

] fields = afterclass.

getdeclaredfields()

;for

(field field : fields)if(

!srcvalue.

equals

(targetvalue))}

return

!modifycontent.

tostring()

.equals(""

)? modifycontent.

tostring()

:"無更新引數";}

private

static object getfieldvalue

(object obj, string fieldname)

string getmethod =

"get"

+ fieldname.

substring(0

,1).

touppercase()

+ fieldname.

substring(1

);method[

] methods = obj.

getclass()

.getdeclaredmethods()

;for

(method method : methods)

trycatch

(exception e)

}return fieldvalue;

}

驗證兩個檔案是否一致

我們常常要驗證兩個檔案是否一致,只需測試兩個檔案的md5是否一致即可。python程式如下 usr bin python coding utf 8 import hashlib import sys def gethash f line f.readline hash hashlib.md5 whi...

比對兩個表的賬單是否一致

sql code 查詢兩個表結構相同,內容之間的差異!insert update,delete create table a 銀行卡號 varchar 5 金額 int,日期 datetime 交易號 varchar 100 insert into aselect x1 100 getdate 00...

python讀取兩個檔案並且判斷是否一致

判斷兩個檔案是否相同,如果不同請指出第幾行不相同 def f1vsf2 name1,name2 f1 open name1 f2 open name2 count 1msg for line1 in f1 line2 f2.readline if line1 line2 第 d行不一樣 count ...