原创文章,转载请注明: 转载自工学1号馆
多态是面向对象最核心的机制,多态使程序的可扩展性达到了极至,这篇文章系统的总结java多态机制
面向对象的三大特性:封装、继承、多态。从一定角度来看,封装和继承几乎都是为多态而准备的。
多态的定义:指允许不同类的对象对同一消息做出响应。即同一消息可以根据发送对象的不同而采用多种不同的行为方式。(发送消息就是函数调用)
实现多态的技术:动态绑定(dynamic binding),是指在执行期间判断所引用对象的实际类型,根据其实际的类型调用其相应的方法。
多态的作用:消除类型之间的耦合关系。
多态存在的三个必要条件:
要有继承、要有重写、父类引用指向子类对象。
不要把函数重载理解为多态。因为多态是一种运行期的行为,不是编译期的行为。
下面举一个例子
public class Employee{
protected String name;
protected int age;
protected double salary;
……
public double getSalary(){
return salary;
}
public double getIncome(){
return salary;
}
}
public class Manager extends Employee{
private double bonus;
……
public double getIncome(){
return salary +bonus;
}
}
public class TestDT {
public static void main(String[] args) {
Employee zhang = new Employee("张三",20,200);
System.out.println(zhang.getSalary());
System.out.println(zhang.getIncome());
Manager wang = new Manager("王五",30,3000,1500);
System.out.println(wang.getSalary());
System.out.println(wang.getIncome());
Employee e1= zhang; //将子类实例赋值给一个父类对象是向上转型
System.out.println(e1.getSalary());
System.out.println(e1.getIncome());
Employee e2 = wang;
System.out.println(e2.getSalary());
System.out.println(e2.getIncome());
}
}
对象调用一个重写方法时,会根据其实例类型调用,而不是根据其对象类型调用。
多态可以让不同子类向上转型到同一个父类:
public class TestClass {
public static void main(String[] args){
Employee[] employees = new Employee[]{
new Employee("张三",20,200),
new Manager("王五",30,3000,1500)};
for(int i = 0; i < employees.length; i++){
System.out.println(employees[i]);
}
}
}
如果没有向上转型……
public class TestClass {
public static void main(String[] args){
System.out.println(new Employee("张三",20,200));
System.out.println(new Manager("王五",30,3000,1500);
}
}
另一种形式的向上转型——形参实现多态
public class CountPay {
private static double pay;
public static void doCount(Employee e){
pay += e.getIncome();
}
}
public class TestClass {
public static void main(String[] args){
CountPay. doCount(new Employee("张三",20,200));
CountPay. doCount(new Manager("王五",30,3000,1500));
}
}
}
如果没有多态,每增加一种职工类型就必须重写编写一个对应的doCount方法:
public class CountPay {
private static double pay;
public static void doCount(Employee e){
pay += e.getIncome();
}
public static void doCount(Manager m){
pay += m.getIncome();
}
}
public class TestClass {
public static void main(String[] args){
CountPay. doCount(new Employee("张三",20,200));
CountPay. doCount(new Manager("王五",30,3000,1500);
}
}
}
多态给程序扩展带来了方便,程序可以对父类进行扩展,而不必修改操作父类的一些方法:
public class CountPay {
private static double pay;
public static void doCount(Employee e){
pay += e.getIncome();
}
}
public class Retire extends Employee{……}
TestClass.doCount(new Retire ());
来看一个完整的例子:
class Shape {
void draw(){
}
}
class Circle extends Shape{
void draw(){
System.out.println("draw circle");
}
}
class Square extends Shape{
void draw(){
System.out.println("draw square");
}
}
public class TestDT1{
public static void main(String[] args) {
Shape s1=new Circle();
s1. draw();
Shape s2=new Square();
s2. draw();
}
}
//
public class TestDT2{
public static void main(String[] args) {
Shape s[]=new Shape[2];
s[0]=new Circle();
s[1]=new Square();
for(int i=0;i<s.length;i++){
s[i]. draw();
}
}
}
//
public class TestDT1{
private static void testDraw(Shape s){
s. draw();
}
public static void main(String[] args) {
Circle c=new Circle();
Square s=new Square();
TestDT1. testDraw(c);
TestDT1. testDraw(s);
TestDT1. testDraw(new Circle());
TestDT1. testDraw(new Square());
}
}
多态的好处:
可替换性(substitutability)。多态对已存在代码具有可替换性。例如,多态对圆Circle类工作,对其他任何圆形几何体,如圆环,也同样工作。
可扩充性(extensibility)。多态对代码具有可扩充性。增加新的子类不影响已存在类的多态性、继承性,以及其他特性的运行和操作。实际上新加子类更容易获得多态功能。例如,在实现了圆锥、半圆锥以及半球体的多态基础上,很容易增添球体类的多态性。
接口性(interface-ability)。多态是超类通过方法签名,向子类提供了一个共同接口,由子类来完善或者覆盖它而实现的。
灵活性(flexibility)。它在应用中体现了灵活多样的操作,提高了使用效率。
简化性(simplicity)。多态简化对应用软件的代码编写和修改过程,尤其在处理大量对象的运算和操作时,这个特点尤为突出和重要。

Comments