首页 > 技术文章 > JAVA多态计算面积main函数调用方法

ye34166684 2016-08-09 08:32 原文

public static void main(String[] args) {
Shape shape;
Scanner input = new Scanner(System.in);
System.out.println("请选择图形(1、圆形 2、矩形 3、三角形)");
int a = input.nextInt();
if(a == 1){
System.out.println("请输入圆形的边长:");
double r = input.nextDouble();
shape = new Round(r);
shape.area();
}else if(a == 2){
System.out.println("请输入矩形的底:");
double bottom = input.nextDouble();
System.out.println("请输入矩形的高:");
double high = input.nextDouble();
shape = new Rectangle(bottom, high);
shape.area();
}else if(a == 3){
System.out.println("请输入三角形的底:");
double bottom = input.nextDouble();
System.out.println("请输入三角形的高:");
double high = input.nextDouble();
shape = new Triangle(bottom, high);
shape.area();
}
input.close();

}

推荐阅读