public abstract class Shape{ public abstract void inputInformation(); public abstract void perimeter(); public abstract void area(); } import java.util.Scanner; public class Rectangle extends Shape{ Scanner input = new Scanner(System.in); public void inputInformation(){ System.out.println("请输入长度和宽度信息: "); } public void area(){ double a = input.nextDouble(); double b = input.nextDouble(); System.out.println("长方形的面积: "+(a*b)); } public void perimeter(){ double a = input.nextDouble(); double b = input.nextDouble(); System.out.println("长方形的周长: "+(2*a+2*b)); } } import java.util.Scanner; public class Circle extends Shape { Scanner input = new Scanner(System.in); public void inputInformation(){ System.out.println("请输入半径长度信息: "); } public void area(){ double a = input.nextDouble(); System.out.println("圆形的面积为: "+(Math.PI*a*a)); } public void perimeter(){ double a = input.nextDouble(); System.out.println("圆形的周长为: "+(Math.PI*a*2)); } } public class Initual{ public static void main(String[] args){ System.out.println("***长方形***"); Shape sh1 = new Rectangle(); sh1.inputInformation(); sh1.area(); sh1.inputInformation(); sh1.perimeter(); System.out.println("----------------------------------------------------"); System.out.println("***圆形***"); Shape sh2 = new Circle(); sh2.inputInformation(); sh2.area(); sh2.inputInformation(); sh2.perimeter(); } }
没有问题啊