//Shape父类
public abstract class Shape {
public float a;
public float b;
public float r;
public abstract void C();
public abstract void S();
public Shape(float a, float b, float r) {
this.a = a;
this.b = b;
this.r = r;
}
}
//矩形子类
public class Rectangle extends Shape {
public void C() {
System.out.println("矩形的周长为:"+(a+b)*2);
}
public void S() {
System.out.println("矩形的面积为:"+a*b);
}
public Rectangle(float a,float b,float r) {
super(a,b,r);
}
}
//圆形子类
public class Circle extends Shape {
public void C() {
System.out.println("圆形的周长为:"+3.14*2*r);
}
public void S() {
System.out.println("圆形的面积为:"+3.14*r*r);
}
public Circle(float a,float b,float r) {
super(a,b,r);
}
}
//输出
import java.util.Scanner;
public class Result {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入矩形的长度:");
String z = input.next();
System.out.println("请输入矩形的宽度:");
String x = input.next();
System.out.println("请输入圆形的半径:");
String c = input.next();
float a = Float.parseFloat(z);
float b = Float.parseFloat(x);
float r = Float.parseFloat(c);
Shape rec = new Rectangle(a, b, r);
Shape cir = new Circle(a, b, r);
rec.C();
rec.S();
cir.C();
cir.S();
}
}