重寫(Override)
重寫是子類重寫父類的方法,如果重寫了父類的方法,訪問時父類的方法就會被覆蓋,如果想要再訪問父類的同名方法,要用super關鍵字。
重寫的好處在于子類可以根據自己的需要,定義特定于自己的行為。
重寫的規則
- 參數列表必須與被重寫的方法完全相同
- 返回類型必須與被重寫的方法的返回類型完全相同
- 訪問權限不能比父類的訪問權限更低。例如,父類的一個方法被聲明為public,那么子類中重寫該方法就不能聲明為protected。
- 父類的成員方法只能被它的子類重寫。
- 聲明為final的方法不能被重寫。
- 聲明為static的方法不能被重寫,但是能被再次聲明。
- 子類和父類在同一個包中,那么子類可以重寫父類所有方法,除了聲明為private和final的方法。
- 子類和父類不在同一個包中,那么子類可以重寫父類聲明為public和protected和非final的方法。
- 重寫的方法能夠拋出任何非強制異常,無論被重寫的方法是否拋出異常。但是,重寫的方法不能拋出新的強制異常,或者比被重寫方法聲明的更廣泛的強制性異常,反之則可以。
- 構造方法不能被重寫。
- 如果不能繼承一個方法,則不能重寫這個方法。
實例如下:
class Animal {public void move() {System.out.println("動物可以移動");}
}class Dog extends Animal {public void move() {System.out.println("狗可以跑和走");}
}public class TestDog {public static void main(String args[]) {Animal a = new Animal(); // Animal 對象Animal b = new Dog(); // Dog 對象a.move();// 執行 Animal 類的方法b.move();// 執行 Dog 類的方法}
}
動物可以移動
狗可以跑和走
在上面的例子中可以看到,盡管b屬于Animal類型,但是它運行的是Dog類的move方法。
這是由于在編譯階段,只是檢查參數的引用類型。
然而在運行時,Java虛擬機(JVM)指定對象的類型并且運行該對象的方法。
因此在上面的例子中,之所以能編譯成功,是因為Animal類中存在move方法,然而運行時,運行的是特定對象的方法。
再看下面的例子:
<pre name="code" class="html">class Animal {public void move() {System.out.println("動物可以移動");}
}class Dog extends Animal {public void move() {System.out.println("狗可以跑和走");}public void bark() {System.out.println("狗可以吠叫");}
}public class TestDog2 {public static void main(String args[]) {Animal a = new Animal(); // Animal 對象Animal b = new Dog(); // Dog 對象a.move();// 執行 Animal 類的方法b.move();// 執行 Dog 類的方法b.bark();}
}
編譯結果:
TestDog.java:30: cannot find symbol
symbol : method bark()
location: class Animalb.bark();
該程序拋出一個錯誤,因為Animal類中沒有bark()方法。
Super關鍵字的使用
當需要在子類中調用父類被重寫的方法是,要使用super關鍵字。
class Bird {public void move() {System.out.println("小鳥會移動");}
}class Swallow extends Bird {public void move() {super.move(); // 應用super類的方法System.out.println("燕子可以飛");}
}public class TestSwallow {public static void main(String args[]) {Bird b = new Swallow(); // Bird對象b.move(); // 執行Swallow類的方法}
}
輸出:
小鳥會移動
燕子可以飛
下面給出一個單純繼承的例子:
public class TestCircle {public static void main(String[] args) {new Circle();}
}class Draw {public Draw(String type) {System.out.println(type + " draw constructor");}
}class Shape {private Draw draw = new Draw("shape");public Shape() {System.out.println("shape constructor");}
}class Circle extends Shape {private Draw draw = new Draw("circle");public Circle() {System.out.println("circle constructor");}
}
shape draw constructor
shape constructor
circle draw constructor
circle constructor
要記住,父類的構造器調用以及初始化過程一定在子類的前面。由于Circle類的父類是Shape類,所以Shape類先進行初始化,然后再執行Shape類的構造器。接著才是對子類Circle進行初始化,最后執行Circle的構造器。重載(Overload)
重載是在同一個類中,方法的名字相同,參數列表不同,返回類型可以相同也可以不同。
每個重載的方法(或構造函數)都必須有一個獨一無二的參數列表。
只能重載構造函數,不能重寫構造函數。
重載規則:
- 被重載的方法必須改變參數列表。
- 被重載的方法可以改變返回類型。
- 被重載的方法可以改變訪問修飾符。
- 被重載的方法可以聲明新的或更廣的檢查異常。
- 方法能夠在同一個類中或者在一個子類中被重載。
public class Overloading {public int test() {System.out.println("test1");return 1;}public void test(int a) {System.out.println("test2");}// 以下兩個參數類型順序不同public String test(int a, String s) {System.out.println("test3");System.out.println(String.format(s, a));// String類的靜態方法format()能用來創建可復用的格式化字符串,而不僅僅是用于一次打印輸出return "returntest3";}public String test(String s, int a) {System.out.println("test4");return "returntest4";}public static void main(String[] args) {Overloading o = new Overloading();System.out.println(o.test());o.test(1);System.out.println(o.test(1, "test3"));System.out.println(o.test("test4", 1));}
}
test1
1
test2
test3
returntest3
test4
returntest4
重寫和重載的區別
區別點 | 重載方法 | 重寫方法 |
---|---|---|
參數列表 | 必須修改 | 一定不能修改 |
返回類型 | 可以修改 | 一定不能修改 |
異常 | 可以修改 | 可以減少或刪除,一定不能拋出新的或者更廣的異常 |
訪問 | 可以修改 | 一定不能做更嚴格的限制(可以降低限制) |
2.重寫只能由一個方法或只能由一堆方法產生關系;重載是多個方法之間的關系。
3.重寫要求參數列表相同;重載要求參數列表不同。