public class Show {
public static void main(String[] args) {
Shape shape1 = new Circle();
Shape shape2 = new Rectangle();
shape1.area();
shape1.grith();
shape2.area();
shape2.grith();
}
}
public abstract class Shape {
public abstract void area();
public abstract void grith();
}
public class Circle extends Shape {
double r ;
static double pi = 3.14;
public void area() {
System.out.print("输入一个半径:");
Scanner scanner = new Scanner(System.in);
int r = scanner.nextInt();
this.r = r;
scanner.close();
double area = pi*r*r;
System.out.println("the circle's area is " + area);
}
public void grith() {
double grith = 2*pi*r;
System.out.println("the grith is " + grith);
}
}
public class Rectangle extends Shape{
static double length = 0;
static double width = 0;
public void area() {
System.out.print("输入长和宽:");
Scanner scanner = new Scanner(System.in);
double length = scanner.nextDouble();
double width = scanner.nextDouble();
scanner.close();
double area = length*width;
System.out.println("the rectangle's area is " + area);
}
public void grith() {
// TODO Auto-generated method stub
double grith = 2*length*width;
System.out.println("the rectangle's grith is " + grith);
}
}