Data Class

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

徵兆與症狀

data class 是指,一個類別只有欄位,其他的方法都是 getter 跟 setter。這種類別只是資料的容器,無法獨立運作。

出現原因

通常新建立的類別只會有少許公開方法(甚至只有 getter 跟 setter),而沒有行為的 data class 沒有到發揮物件導向的威力。

處理方法

  • 如果類別有公開欄位,用 Encapsulate Record 將欄位先封裝起來,某些不會被更改的欄位,使用 Remove Setting Method 來移除。
  • 看看其他類別使用 data class 的程式碼,嘗試用 Move Function 將功能搬到 data class,若沒有辦法直接搬功能,可以先 Extract Function 後再進行搬移。