多態(tài)的存在有三個前提:
1.要有繼承關(guān)系
2.子類要重寫父類的方法
3.父類引用指向子類對象
看下面幾個例子,你就全明白了
第一個例子,僅有一個speak()方法
public class test {
public static void main(String[] args) {
father people = new son();
people.speak();
}
}
class father{
public void speak() {
System.out.println("i am father !");
}
}
class son extends father{
public void speak(){
System.out.println("i am son !");
}
}
輸出:
i am son !
第二個例子,多了一個age
public class test {
public static void main(String[] args) {
father people = new son();
people.speak();
System.out.println("i am "+people.getAge());
}
}
class father{
private int age ;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void speak() {
System.out.println("i am father !");
}
}
class son extends father{
public son() {
setAge(17);
}
public void speak(){
System.out.println("i am son !");
}
}
輸出:
i am son !
i am 17
第三個例子,son中加了個play()
public class test {
public static void main(String[] args) {
father people = new son();
people.speak();
System.out.println("i am "+people.getAge());
people.play();
}
}
class father{
private int age ;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void speak() {
System.out.println("i am father !");
}
}
class son extends father{
public son() {
setAge(17);
}
public void speak(){
System.out.println("i am son !");
}
public void play(){
System.out.println("i am son , i want to play!");
}
}
輸出:
<b>Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method play() is undefined for the type father
at com.data.model.test.main(test.java:11)</b>
很明顯,father中缺少play()方法,所以不能執(zhí)行。
第四個例子,animal
這里要介紹一個綁定的概念
1、將一個方法調(diào)用同一個方法主體關(guān)聯(lián)起來 稱作 綁定
- 如果在程序執(zhí)行前進行綁定,叫做前期綁定(由編譯器和連接程序?qū)崿F(xiàn))
- 而另外一種在程序執(zhí)行時綁定的,叫做動態(tài)綁定,也叫后期綁定,運行時綁定
- 動態(tài)綁定也就是說編譯器一直不知道對象的類型,但是可以根據(jù)方法調(diào)用機制找到正確的方法體
2、那么java中有哪些方法是屬于動態(tài)綁定的呢?
- 除了static 、final方法之外(private方法也屬于final方法),其他所有的方法都是后期綁定
- 也就是說 static 、final 方法都不具有多態(tài)性!
- 在編譯時,編譯器不需要獲得任何特殊信息就能進行正確的調(diào)用
3、為什么要將某個方法聲明為final 呢?
- 為了要防止重寫,可以有效的關(guān)閉動態(tài)綁定
4、多態(tài)的優(yōu)點 - 可以忽略繼承的類的變化,依舊可以正常運行,將改變的事物與圍邊的事物分離開來
class Animal{
public void speak(){
System.out.println("i am animal. i am speaking");
}
public void run(){
System.out.println("i am animal , i am running");
}
}
class Cat extends Animal{
public void speak(){
System.out.println("miao miao miao ~~");
}
}
public class Polymorphic {
/**
* 這樣就不用給每個animal編寫方法了
* 只要用這樣的一個方法,就可以調(diào)用繼承這個animal的類的所有重寫的方法
*/
public static void testAnimal(Animal animal){
animal.speak();
}
public static void main(String[] args) {
Cat cat = new Cat();
testAnimal(cat);
}
}
我們可以適當(dāng)修改程序
public static void main(String[] args) {
Animal cat = new Cat();// 將Cat改成Animal
testAnimal(cat);
}
再深入,當(dāng)我們建立一個新的類WhiteCat 繼承Cat 時,也能夠正確的調(diào)用
class WhiteCat extends Cat{
public void speak(){
System.out.println("miao~ miao~");
}
}
public class Polymorphic {
public static void testAnimal(Animal animal){
animal.speak();
}
public static void main(String[] args) {
Animal whiteCat = new WhiteCat();
testAnimal(whiteCat);
}
}
缺陷:域和靜態(tài)方法
1、靜態(tài)方法不具有多態(tài)性。
靜態(tài)方法是與類相關(guān)聯(lián)的
class Animal{
public static void eat(){
System.out.println("animal eat");
}
}
class Cat extends Animal{
public static void eat(){
System.out.println("cat eat");
}
}
public class Polymorphic {
public static void main(String[] args) {
Animal cat = new Cat();
cat.eat();
}
}
輸出:
animal eat
2、下面的舉例是域的缺陷
- 當(dāng)我們在每個class中都定義不同的age時
class Animal{
public int age = 1;
public void speak(){
System.out.println("i am animal. i am speaking");
}
public void run(){
System.out.println("i am animal , i am running");
}
}
class Cat extends Animal{
public int age = 2;
public void speak(){
System.out.println("miao miao miao ~~");
}
}
class WhiteCat extends Cat{
public int age = 3;
public void speak(){
System.out.println("miao~ miao~");
}
}
public class Polymorphic {
/**
* 這樣就不用給每個animal編寫方法了
* 只要用這樣的一個方法,就可以調(diào)用繼承這個animal的類的所有重寫的方法
*/
public static void testAnimal(Animal animal){
animal.speak();
}
public static void main(String[] args) {
Animal cat = new Cat();
testAnimal(cat);
System.out.println(cat.age);
Animal whiteCat = new WhiteCat();
testAnimal(whiteCat);
System.out.println(whiteCat.age);
}
}
我們會發(fā)現(xiàn)輸出的age 都是1
輸出:
miao miao miao ~~
1
miao~ miao~
1
- 所以我們一般為了不混淆,會將 這個age定義為私有變量,可以提供getAge()方法訪問它