麻烦老师看下,哪里还需要改进
相关代码:
/**
* @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
2023版Java工程师
- 参与学习 人
- 提交作业 8789 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星