链接:https://www.nowcoder.com/questionTerminal/699ba050e2704591ae3e62401a856b0e?answerType=1&f=discussion
来源:牛客网

KiKi理解了继承可以让代码重用,他现在定义一个基类shape,私有数据为坐标点x,y, 由它派生Rectangle类和Circle类,它们都有成员函数GetArea()求面积。派生类Rectangle类有数据:矩形的长和宽;派生类Circle类有数据:圆的半径。Rectangle类又派生正方形Square类,定义各类并测试。输入三组数据,分别是矩形的长和宽、圆的半径、正方形的边长,输出三组数据,分别是矩形、圆、正方形的面积。圆周率按3.14计算

输入描述:

输入三行,
第一行为矩形的长和宽,
第二行为圆的半径,
第三行为正方形的边长。

输出描述:

三行,分别是矩形、圆、正方形的面积。
import java.util.Scanner;
public class Main{
public static void main(String[]args){
Scanner s=new Scanner(System.in);
while(s.hasNext()){
Rectangle rectangle=new Rectangle(s.nextInt(),s.nextInt());
Circle circle=new Circle(s.nextDouble());
Square square=new Square(s.nextInt());
System.out.println(rectangle.GetArea());
double rad=circle.GetArea();
double k = rad-(int)rad;
if (k == 0.0)
System.out.println((int)rad);
else
System.out.println(rad);
System.out.println(square.GetArea());
}
}
}
class shape{
private int x;
private int y;
}
class Rectangle extends shape{
private int length;
private int width;
public Rectangle(int length,int width){
this.length=length;
this.width=width;
}
public int GetArea(){
return lengthwidth;
}
}
class Square extends Rectangle{
public Square(int length){
super(length,length);
}
}
class Circle extends shape{
private double radius;
public Circle(double radius){
this.radius=radius;
}
public double GetArea(){
return radius
radius*3.14;
}
}