「Java」「enum」 Chapter4 Question16

Given:
enum TWMoney{
 Ten(10),Hundred(100),Thousand(1000);
 private int worth;
 public TWMoney(int i) {
  this.worth = i;
 }
 public int getWorth() {
  return worth;
 }
}
public class Test5 {
 public static void main(String[] args) {
  TWMoney curr = new TWMoney.Hundred;
  System.out.println(curr.getWorth());
 }
}


Answer:

enum列舉中,建構子必須是private。
所以修改為: public TWMoney(int i)

要實作列舉中的欄位,不可用new
修改為:  TWMoney curr = TWMoney.Hundred;


留言

  1. Stainless Steel PS4 Controller
    The Original Sega Genesis ford escape titanium for sale Controller with 8-Button Arcade Pad. The Original Sega Genesis Controller titanium prices with 8-Button Arcade Pad. mens black titanium wedding bands 8-Button toaks titanium Controller for the titanium mens wedding bands Sega Genesis Mini.

    回覆刪除

張貼留言

這個網誌中的熱門文章

[Excel]國曆轉農曆VBA

「CSS」「div區塊介紹」 三欄式網頁排版設計

「生活」眉毛觀人