java多态

c++使用virtual实现多态,是一种手动动态绑定的过程。在java中,动态绑定是默认行为,不需要添加额外的关键字来实现多态。

具体实例:

 1 class Shape{
 2     
 3     void erase(){
 4         System.out.println("erase");
 5     }
 6     
 7     void draw(){
 8         System.out.println("draw");
 9     }
10 }
11 
12 class Circle extends Shape{
13     void erase(){
14         System.out.println("Circle erase");
15     }
16     void draw(){
17         System.out.println("Circle draw");
18     }
19 }
20 
21 public class Test {
22     
23     public static void doSomething(Shape shape){
24         shape.erase();
25         shape.draw();
26     }
27     
28     public static void main(String[] args) {
29         // TODO Auto-generated method stub
30         Circle circle = new Circle();
31     
32             doSomething(circle);
33     }
34 }

输出:

Circle erase
Circle draw

原文地址:https://www.cnblogs.com/xuxu8511/p/2516482.html