麻烦老师看下,哪里还需要改进
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | 相关代码: /** * @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积分~
来为老师/同学的回答评分吧