麻烦老师看下,哪里还需要改进
相关代码: /** * @ClassName ShapeTest * @description: Shape的测试类 * @author: 自己名字 * @create: 2022-03-23 16:44 **/ public class ShapeTest { public static void main(String[] args) { //创建类的实例,并分别对圆的半径和矩形的长宽进行赋值 Shape circle1=new Circle(3.0); Shape rectangle1=new Rectangle(2.0,6.0); //调用area()方法,输出结果 System.out.println("圆形的面积为:"+circle1.area()); System.out.println("长方形的面积为:"+rectangle1.area()); } } /** * @ClassName Shape * @description: 父类 * @author: 自己名字 * @create: 2022-03-23 16:42 **/ public abstract class Shape { public Shape(){ } public abstract double area(); } /** * @ClassName Circle * @description: Shape的子类 * @author: 自己名字 * @create: 2022-03-23 16:43 **/ public class Circle extends Shape{ //属性:圆的半径r private double r; //创建带参构造方法以及无参构造方法 public Circle(){ } public Circle(double r){ this.setR(r); } //创建针对半径的赋值和取值方法 public void setR(double r){ this.r=r; } public double getR(){ return r; } //重写父类中area()方法 @Override public double area() { return Math.PI*(getR()*getR()); } } /** * @ClassName Rectangle * @description: Shape的子类 * @author: 自己名字 * @create: 2022-03-23 16:43 **/ public class Rectangle extends Shape { //属性:矩形的长lenghth、宽wide private double length; private double wide; //创建带参构造方法以及无参构造方法 public Rectangle(){ } public Rectangle(double length,double wide){ this.setLength(length); this.setWide(wide); } //创建针对长、宽的赋值和取值方法 public void setLength(double length){ this.length=length; } public double getLength(){ return length; } public void setWide(double wide){ this.wide=wide; } public double getWide(){ return wide; } //重写父类的area()方法 @Override public double area() { return this.getLength()*this.getWide(); } }
29
收起
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星