爲SCJP認證考試而努力-4

    寫博客這幾天終於有朋友回帖了,心裏高興。下面學習默認的構造方法、方法的重載和覆寫,以下代碼可以直接在MyEclipse中運行,到最後我會把中間寫的所有代碼打包放出來,敬請關注。
package operation;
class ConsBase {
	ConsBase(int i) {
		System.out.println("single int constructor");
	}
	/**
	 * 如果你沒有顯式定義任何構造方法,編譯器會插入一個“後臺”的不可見的無參數的構造方法。
	 * 如果你自己創建了構造方法,Java	就不支持默認的無參數的構造方法了。
	 * 可以通過在Base 類中創建一個“什麼都不幹”的零參數構造方法來修復這個錯誤。
	 */
	ConsBase() {// 構造器有和類同樣的名字並且沒有返回值
		System.out.println("ConsBase");
		// 當類的實例被創建時打印字符串“ConsBase”:
	}
	public void aBaseMethod(){
		System.out.println("Base.amethod");
	}
}
/**
 * @author 木炭
 * 程序最後輸出:ConsBase 換行 Cons
 */
public class Cons extends ConsBase {
	/**
	 * @param args
	 */
	public static void main(String args[]) {
		// ConsBase c = new ConsBase();
		Cons s = new Cons();
	}
	Cons() {
		System.out.println("Cons");
	}
	
	/**
	 * @param iOver
	 * 如果一個類中的兩個或者多個方法有同樣的名字,就被稱爲方法重載。
	 * 你可以在一個類中有兩個同樣名字的方法,但是他們必須有不同的參數類型和順序。
	 * 返回值的類型不能幫助區分兩個方法。下面第二個參數相同的方法將導致編譯時錯誤。
	 */
	public void amethod(int iOver){
	System.out.println("Cons.amethod");
	}
/*	public int amethod(int iOver){
	System.out.println("Cons int return method");
	return 0;
	}*/
	//正確的重構
	public int amethod(String iOver){
		System.out.println("Cons int return method");
		return 0;
	}
	//你可以在一個子類中重載一個方法,所需要的就是新方法有不同的參數順序和類型。
	//static 方法不能被重寫。
	public void aBaseMethod(){
		System.out.println("重寫aBaseMethod成功");
	}
	
}
問題 ) 給定下面的類定義
class Base{
Base(int i){}
}
class DefCon extends Base{
DefCon(int i){
//XX}
}
如果將標記//XX 的地方替換爲下面的行,哪一行是獨立合法的?
1) super();
2) this();
3) this(99);
4)super(99);
答案 4) super(99);
由於類Base 定義了一個構造方法,編譯器將不會插入默認的0 參數的構造方法。因此,super()
的調用會引起一個錯誤。一個this()調用試着在當前類中調用一個不存在的0 參數構造方法,
this(99)調用會引起一個循環引用並將引起一個編譯時錯誤。
問題 )下面的哪一句陳述是正確的?
1) The default constructor has a return type of void
2) The default constructor takes a parameter of void
3) The default constructor takes no parameters
4) The default constructor is not created if the class has any constructors of its own.
答案
3) The default constructor takes no parameters
4) The default constructor is not created if the class has any constructors of its own.
選項1 相當明顯,因爲構造方法不會有返回類型。選項2 不容易確定,Java 沒有爲方法或構
造方法提供void 類型。
問題 )給定下面的類定義
public class Upton{
  public static void main(String argv[]){
  }
  public void amethod(int i){}
  //Here
}
下面哪一個在替換//Here 後是合法的?
1) public int amethod(int z){}
2) public int amethod(int i,int j){return 99;}
3) protected void amethod(long l){ }
4) private void anothermethod(){}
答案
2) public int amethod(int i, int j) {return 99;}
3) protected void amethod (long l){}
4) private void anothermethod(){}
選項1 由於兩個原因不會被編譯。第一個相當明顯,因爲它要求返回一個integer。
另一個是試着直接在類內部重新定義一個方法。把參數的名字從i 換成z 是無效的,類型沒有變。
並且一個方法不能在同一個類裏重寫。
問題 )給定下面的類定義
public class ShrubHill{
public void foregate(String sName){}
//Here
}
下面的哪一個方法可以合法的直接替換//Here?
1) public int foregate(String sName){}
2) public void foregate(StringBuffer sName){}
3) public void foreGate(String sName){}
4) private void foregate(String sType){}
答案
2) public void foregate(StringBuffer sName){}
3) public void foreGate(String sName){}
選項1 是試着定義一個方法兩次,有一個int 返回值並不能幫助將它與存在的foregate 方法相區分。
而像選項4 那樣改變方法的參數名,也不能與存在的方法相區分。
選項2裏的foreGate 方法是與foregate不同的方法。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章