P.94
Method signature (Method 特徵)包含:
method名稱
參數數量
參數資料型態
參數前後順序
P.95
一般在收參數時,會寫成這樣:
public float average(int x1, intx2){}
但如果一次要收多個參數,可以寫成:
P.111
子類別可覆寫父類別,前提是:
- 靜動態宣告, method名稱, 參數數量、參數型態、參數順序需與父類別相同 //Extend父類別之後,完全照抄父類別METHOD,那一定可以覆寫
- 存取權限與能與父類別相同或更大 //或是最前面的權限形容詞private, 則子類別用public可覆寫
- 回傳值型態如果是基本型態則須與父類別相同, 如果回傳值是物件型態, 則可與父類別回傳值相同或更小 //父類別是Object, 子類別是String可覆寫
- 子類別method拋出的應檢核例外需與父類別應檢核例外相同或更小, 非檢核例外則可與父類別相同或不同
Overload(多載)考題:
(8) Given:
1. public class ConstOver {
2. public ConstOver(int x, int y, int z){
3. }
4. }
Which two overload the ConstOver constructor? (Choose Two)
A. ConstOver( ){ }
B. protected int ConstOver( ){ }
C. private ConstOver(int z, int y, byte x){}
D. public void ConstOver(byte x, byte y, byte z){}
E. public Object ConstOver(int x, int y, int z){}
答案:A, C Overload的題目只需要關心method能否和平相處
(9) Given:
1. public class MethodOver {
2. public void setVar(int a, int b, float c){
3. }
4. }
Which two overload the setVar method ? (Choose Two)
A. private void setVar(int a, float c, int b){ }
B. protected void setVar(int a, int b, float c){ }
C. public int setVar(int a, float c, int b){return a;}
D. public int setVar(int a, int b, float c){return a;}
E. protected float setVar(int a, int b, float c){return c;}
答案: A,C 只要看參數宣告及型態就好 1.先看SetVar, 全部符合 2.再看 int, int, float, 選項BDE都一樣,淘汰, 3.C OK
Override(覆寫)考題:
(10) Given:
1. class BaseClass {
2. private float x = 1.0f ;
3. protected float getVar( ){return x;}
4. }
5. class Subclass extends BaseClass {
6. private float x = 2.0f;
7. //insert code here
8. }
Which two are valid examples of method overriding ? (Choose Two)
A. float getVar( ){return x;}
B. public float getVar( ){return x;}
C. float double getVar( ){return x;}
D. protected float getVar( ){return x;}
E. public float getVar(float f ){return f;}
答案:B,D 完全一樣或,權限較大,回傳值較小,參數宣告一樣。
權限大小,由小到大:
1. private權限 - 僅供同class內所有method存取.
2. package權限 - 僅供同package中的所有class內的所有method皆可存取.
3. protected權限 - 供同package中的所有class內的所有method皆可存取, 或同package或不同package中的子類別存取
4. public權限 - 開放所有class的所有method皆可存取.
Static Inputs
以前要呼叫JOptionPane, 要用JOptionPane.showDialog, 但Java 5.0後,可以寫import static java.lang.JOPtionPane.*,這樣後面就可以直接 showDialog();
P.159 ENUM
public enum Gender {
MALE("男生"), FEMALE("女生"), OTHER("其他");
// 上面列舉值編譯完將會變成如下宣告
// public static final Gender MALE = new Gender("男生");
// public static final Gender FEMALE = new Gender("女生");
// public static final Gender OTHER = new Gender("其他");
private String name;
private Gender(String name) {
this.name = name;
}
private Gender() {}
@Override
public String toString() {
return name;
}
}
public class Main {
public static void main(String[] args) {
Human andy = new Human("劉德華", Gender.MALE); //Gender.MALE的Gender可以被省略,只要上方加入import static
Human grace = new Human("關芝靈", Gender.FEMALE);
Human joseph = new Human("劉德華", Gender.MALE);
}
}
import static
public class Main {
public static void main(String[] args) {
Human andy = new Human("劉德華", Gender.MALE);
Human grace = new Human("關芝靈", Gender.FEMALE);
Human joseph = new Human("劉德華", Gender.MALE);
}
}
考題ENUM:
class Test {
public enum Direction { NORTH, SOUTH, EAST, WEST; }
public class Sprite {
// insert code here
}
}
Which code, inserted at "// insert code here", allows the Sprite class to compile? (choose two)
allows 允許
A. Direction d = NORTH;
B. Test.Direction d = NORTH;
C. Direction d = Direction.NORTH;
D. Test.Direction d = Test.Direction.NORTH;
答案: C,D AB等號右邊,沒有路徑,只會在Sprite裡找, 找不到。CD的Test.加不加無彷
考題ENUM:
enum Example { ONE, TWO, THREE; }
Which statement is true?
A. The expressions (ONE == ONE) and ONE.equals(ONE) are both guaranteed to be true.
B. The expression (ONE < TWO ) is guaranteed to be true and ONE.compareTo (TWO) is guaranteed to be less than one.
C. The Example values cannot be used in a raw java.util.HashMap.; instead, the programmer must use a java.util.EnumMap.
D. The Example values can be used in a java.util.SortedSet, but the set will NOT be sorted because enumerated Type do NOT implement java.lang.Comparable.
答案:A B的(ONE<TWO)一定是錯的,因為兩個其實是物件,不能比大小
考題ENUM:
public class Test {
public enum Dogs { collie, harrier, shepherd };
public static void main(String[] args) {
Dogs myDog=Dogs.shepherd;
switch(myDog) {
case collie: //原廠規定 case後面只能寫值(collie),不能寫路徑Dog. (不像ENUM一定要)
System.out.print("collie ");
case default:
System.out.print("retriever ");
case harrier:
System.out.print("harrier ");
}
}
}
What is the result?
A. harrier
B. shepherd
C. retriever
D. Compilation fails
E. retriever harrier
F. An exception is thrown at runtime.
答案: D,default前面不用加"case",如果拿掉答案就變成E。