多态(polymorphism)

多态的概念

·多态是指一个程序中相同的名字表示不同的含义的情况。

·多态有两种情形:

         1、编译时多态:

                   1.重载(overload)多个同名的不同方法;

                   2.如:p.sayHello();p.sayHello(“wang”);

         2、运行时多态:

                   1.覆盖(override)子类对父类方法进行覆盖

                   2.动态绑定(dynamic binding)也称为虚方法调用(virtual method invoking),真正的方法在运行时才确定

                   3.在调用方法时,程序会正确地调用子类对象的方法。

·多态的特点大大提高了程序的抽象程度和简洁性。

 

上溯造型(upcasting)

·是把派生类型当作基本类型处理,如:

Person p = new Sudent();

void fun(Person p){…}
fun(new Person)); or fun(new Student());
View Code

相关文章:

  • 2021-11-15
  • 2022-12-23
  • 2021-12-28
  • 2021-09-05
  • 2021-05-21
  • 2021-11-18
猜你喜欢
  • 2021-09-28
  • 2022-12-23
  • 2021-07-05
  • 2022-12-23
  • 2022-12-23
  • 2022-02-09
相关资源
相似解决方案