Java中的动态和静态多态有什么区别? [英] What is the difference between dynamic and static polymorphism in Java?
本文介绍了Java中的动态和静态多态有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人能提供一个简单的例子来解释Java中动态和静态多态的区别吗?
Can anyone provide a simple example that explains the difference between Dynamic and Static polymorphism in Java?
推荐答案
多态性
Polymorphism
1。静态绑定/编译时绑定/早期绑定/方法重载。(在同一类中)
2。动态绑定/运行时绑定/后期绑定/方法覆盖。(在不同的类中)
class Calculation {
void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}
public static void main(String args[]) {
Calculation obj=new Calculation();
obj.sum(10,10,10); // 30
obj.sum(20,20); //40
}
}
覆盖示例:
overriding example:
class Animal {
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move();//output: Animals can move
b.move();//output:Dogs can walk and run
}
}
这篇关于Java中的动态和静态多态有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文