專業英語其實不難:語法簡單、詞彙多為高中級、專業術語有限且高頻。掌握核心詞彙能直接閱讀一手技術文檔、源碼註釋、國際社區討論。
| 中文 | 英文 | 發音提示 |
|---|---|---|
| 封裝 | Encapsulation | /ɪnˌkæpsjʊˈleɪʃn/ |
| 繼承 | Inheritance | /ɪnˈherɪtəns/ |
| 多態 | Polymorphism | /ˌpɒlɪˈmɔːfɪzəm/ |
| 實參 | Arguments | 調用時傳入的具體值 |
| 形參 | Parameters | 方法定義時的變量 |
public class Person {
private int age;
public void setAge(int age) {
if (age < 0) throw new IllegalArgumentException("年齡不能為負");
this.age = age;
}
}
外部無法繞過 setAge 直接賦值 -100。
The point to setters (and getters) is that you can change your mind later, without breaking anybody else's code!
遊戲充值下限從 0.1 元調整為 100 元:
若屬性 public,所有調用處需加 if 判斷;
若封裝 setPrice() 內部邏輯,僅修改一處,全局生效。
| 場景 | == | equals() |
|---|---|---|
| 基本類型 | 比較值 | 不適用 |
| 引用類型 | 比較地址值(是否同一對象) | 比較內容(需類重寫) |
String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
s1 == s2 // true (常量池複用)
s1 == s3 // false (堆上新對象)
s1.equals(s3) // true (內容相同)
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}
@Override public int hashCode() { return Objects.hash(name, age); }
Java/C/C++ 只能返回單值。想返回多值:
1. 封裝為對象/數組/集合。
2. Python 看似返回多值,實則返回 tuple(元組)。
When everything seems to be going against you, remember that the airplane takes off against the wind, not with it.
只有逆風,才能起飛。堅持寫代碼,哪怕每天多敲十行。