/*** @Description 什么是多態*/
package com.oop;import com.oop.demo06.Person;
import com.oop.demo06.Student;public class Application {public static void main(String[] args) {//一個對象的實際類型是確定的//new Student();//new Person();//可以指向的引用類型就不確定了:父類的引用指向子類//Student 能調用的方法都是自己的或者繼承父類的!Student s1 = new Student();//Person 父類型,可以指向子類,但是不能調用子類獨有的方法Person s2 = new Student();Object s3 = new Student();//對象能執行那些方法,主要看對象左邊的類型,和右邊關系不大!5s2.run();//子類重寫了父類的方法,執行子類的方法s1.run();}}
/*** @Description 什么是多態*/
package com.oop.demo06;public class Student extends Person{@Overridepublic void run(){System.out.println("son");}public void eat(){System.out.println("ear");}}
/*** @Description 什么是多態*/
package com.oop.demo06;public class Person {public void run(){System.out.println("run");}}/*
多態注意事項:
1.多態是方法的多態,屬性沒有多態
2.父類和子類,有聯系 類型轉換異常! ClassCastException!
3.存在條件:繼承關系,方法需要重寫,父類引用指向子類對象! father f1 = new Son();1.static 方法,屬于類,它不屬于實例2.final 常量:3. private:*/