背景
使用 IDEA 開發時黃色預警:
This inspection searches for redundant class fields that can be replaced with local variables,if all local usages of a field are preceded by assignments to that field,the field can be removed and its usages replaced with local variables.
該變量在類中使用比較少,一般只在一個函數中使用,不需要創建一個全局變量,可以在調用該變量的函數中,使用時創建局部變量。
解決方案
按照上面的說法,可以對代碼進行處理。例如:
修改前代碼如下,由於mContext變量只是在構造函數中被調用,所以出現提示“Field can be converted to a local varible”