Linkedin Java 技術認證題庫 日期/ 建構子


Posted by c9103205 on 2021-07-05

Which is the most up-to-date way to instantiate the current date?

  1. new SimpleDateFormat("yyyy-MM-dd").format(new Date())
  2. new Date(System.currentTimeMillis())
  3. LocalDate.now()
  4. Calendar.getInstance().getTime()

Answer:3
筆者這題完全就敗在英文,"up-to-date" 意思就是 最新的!
Java中新增日期的方法有很多種
LocalDate 是Java8 新增的class
結果知道單字好像也沒辦法答題,除非你是Java歷史小老師...哈哈

public class Jedi {
  /* Constructor A */
  Jedi(String name, String species){}

  /* Constructor B */
  Jedi(String name, String species, boolean followsTheDarkSide){}
  }
  1. Jedi(name, species, false)
  2. new Jedi(name, species, false)
  3. this(name, species, false)
  4. super(name, species, false)

answer: 3
這題說要怎麼樣在"這個"建構子A中呼叫建構子B
Jedi(name, species, false) : 沒這種用法,編譯不過,注意宣告建構子的method跟一般的method不一樣
一般的方法要宣告回傳值或void,建構子方法則不用

new Jedi(name, species, false) : 這意思 new 一個 Jedi 物件,並把參數傳入
因為new 了就不再是 "這個"物件,所以這個也不能選

this(name, species, false) : this在java中表示"這個class" ,所以這個是正確答案
super(name, species, false) : super 如果放在建構子的第一行,表示呼叫父類別,雖然本class沒有繼承任何父類別,但在Java中會預設所有類別都是Object的子類別,同時Object沒有對應的method輸入參數是(String,String,boolean),所以也會編譯不過


#linkedin #java







Related Posts

API

API

shell script in pre-commit

shell script in pre-commit

#001_0612 始まる。

#001_0612 始まる。


Comments