java獲取當前方法的名稱

2021-08-21 14:35:04 字數 617 閱讀 2751

string clazz = this.getclass().getname();

獲取當前類的 類名

string method = thread.currentthread() .getstacktrace()[1].getmethodname();

獲取當前方法

返回乙個陣列 thread.currentthread() .getstacktrace()這個方法儲存當前執行緒的棧快照

如果有以下呼叫、

main()------>show()------->othershow() 在此處呼叫後 thread.currentthread() .getstacktrace()

那麼裡面的內容

[0] --------->getstacktrace()

[1]----------->othershow()

[2]------------>show()

[3]------------> main()

這個方法 可以在任何方法裡面呼叫 不管是靜態方法 還是例項方法

最後截個圖 給你看下效果

獲取呼叫當前方法的方法

thread.currentthread getstacktrace 1 是你當前方法執行堆疊 thread.currentthread getstacktrace 2 就是上一級的方法堆疊 以此類推 stacktraceelement temp thread.currentthread getst...

獲取呼叫當前方法的類名

第一種常規方式 stacktraceelement stack new throwable getstacktrace for int i 0 i stack.length i view code stacktraceelement stack new throwable getstacktrace...

c 獲取當前方法事件對應的控制項

例如,combobox1對應的事件selectionchangecommitted private void combobox1 selectionchangecommitted object sender,eventargs e 可以通過selectionchangecommitted事件中傳遞過...