package com.imooc;
public abstract class Shape {
public abstract void perimeter();//求周长
public abstract void area();//求面积
}
package com.imooc;
import java.util.Scanner;
public class Rectangle extends Shape {
public void perimeter() {
Scanner input = new Scanner(System.in);
System.out.println("请输入矩形的宽:");
float width = input.nextFloat();
System.out.println("请输入矩形的长:");
float length=input.nextFloat();
System.out.println("矩形的周长为:"+2*(width+length));
}
public void area() {
Scanner input = new Scanner(System.in);
System.out.println("请输入矩形的宽:");
float width=input.nextFloat();
System.out.println("请输入矩形的长:");
float length=input.nextFloat();
System.out.println("矩形的面积为:"+width*length);
}
}
package com.imooc;
import java.util.Scanner;
public class Circle extends Shape {
public void perimeter() {
Scanner input = new Scanner(System.in);
System.out.println("请输入圆形的半径:");
float radius=input.nextFloat();
System.out.println("圆形的周长为:"+radius*2*3.14);
}
public void area() {
Scanner input = new Scanner(System.in);
System.out.println("请输入圆形的半径:");
float radius=input.nextFloat();
System.out.println("圆形的面积:"+3.14*radius*radius);
}
}
package com.imooc;
public class Initial {
public static void main(String[] args) {
Shape s1=new Rectangle();
s1.perimeter();
s1.area();
Shape s2=new Circle();
s2.perimeter();
s2.area();
}
}