有谁能解释下多态的用处在哪呢?自己不太明白
//Traffic类
package com.edu;
public class Traffic {
public String TrafficName; //交通工具的名称
public int maxNum; //交通工具的最大承载量
public void showData(){
System.out.println(TrafficName+"这种交通工具最多允许乘坐:"+maxNum+"人");
}
}
//Car类
package com.edu;
public class Car extends Traffic{
public Car(int max){
TrafficName = "汽车";
maxNum = max;
}
}
//Boat类
package com.edu;
public class Boat extends Traffic{
public Boat(int max){
TrafficName = "小船";
maxNum = max;
}
}
//Intial类
package com.edu;
import java.util.Scanner;
public class Initial {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Traffic tool = new Traffic();
System.out.print("请输入交通工具的名称:");
Scanner input = new Scanner(System.in);
String TrafficName = input.next();
System.out.print("请输入交通工具的最大乘载量:");
int maxNum = input.nextInt();
if(TrafficName.equals("汽车")){
tool = new Car(maxNum);
}else if(TrafficName.equals("小船")){
tool = new Boat(maxNum);
}
tool.showData();
}
}