From e328b915649790132b79837602632e069135d2f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E8=82=96=E9=92=9F=E5=87=AF=E9=9F=A9=E2=80=9D?= <“3175644391@qq.com”> Date: Mon, 8 May 2023 23:56:39 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E5=9B=9B=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\346\200\201\344\275\234\344\270\232.md" | 210 ++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 "22 \350\202\226\351\222\237\345\207\257\351\237\251/20230508 \345\244\232\346\200\201\344\275\234\344\270\232.md" diff --git "a/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230508 \345\244\232\346\200\201\344\275\234\344\270\232.md" "b/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230508 \345\244\232\346\200\201\344\275\234\344\270\232.md" new file mode 100644 index 0000000..77cd079 --- /dev/null +++ "b/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230508 \345\244\232\346\200\201\344\275\234\344\270\232.md" @@ -0,0 +1,210 @@ +# 作业 + +~~~ java +// 父类 +package demo02; + +public abstract class Graphic { +// - public double area()方法:返回0.0 +// - public double perimeter()方法:返回0.0 +// - public String getInfo()方法,返回图形面积和图形周长 + private String name; + + public Graphic(String name) { + this.name = name; + } + + public Graphic() { + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double area(){ + return 0.0; + } + public double perimeter(){ + return 0.0; + } + public String getInfo(){ + return null; + } +} + +~~~ + +~~~ java +// 子类 +package demo02; +// 圆形 +public class Circle extends Graphic{ +//- 包含属性:radius,属性私有化 +//- 包含get/set方法 +//- 重写area()求面积方法 +//- 重写perimeter()求周长方法 +//- 重写getInfo()方法,返回圆的半径,面积和周长 + private double radius; + + public Circle(String name, double radius) { + super(name); + this.radius = radius; + } + + public Circle(double radius) { + this.radius = radius; + } + + public Circle() { + } + + public double getRadius() { + return radius; + } + + public void setRadius(double radius) { + this.radius = radius; + } + + @Override + public double area() { + return Math.PI*radius*radius; + } + + @Override + public double perimeter() { + return Math.PI*(radius*2); + } + + @Override + public String getInfo() { + return getName()+"的半径为:"+radius+"\t\t面积为:"+area()+"\t\t\t周长为:"+perimeter(); + } +} + +~~~ + +~~~ java +// 子类 +package demo02; +// 矩形 +public class Rectangle extends Graphic{ +//- 包含属性:length、width,属性私有化 +//- 包含get/set方法 +//- 重写area()求面积方法 +//- 重写perimeter()求周长方法 +//- 重写getInfo()方法,返回长和宽,面积、周长信息 + private double length; + private double width; + + public Rectangle(String name, double length, double width) { + super(name); + this.length = length; + this.width = width; + } + + public Rectangle(double length, double width) { + this.length = length; + this.width = width; + } + + public Rectangle() { + } + + public double getLength() { + return length; + } + + public void setLength(double length) { + this.length = length; + } + + public double getWidth() { + return width; + } + + public void setWidth(double width) { + this.width = width; + } + + @Override + public double area() { + return length*width; + } + + @Override + public double perimeter() { + return 2*(length+width); + } + + @Override + public String getInfo() { + return getName()+"的长为:"+length+ + "\t\t宽为:"+width+ + "\t\t面积为:"+area()+ + "\t\t周长为:"+perimeter(); + } +} + +~~~ + +~~~ java +// 测试类 +package demo02; + +public class Test { + public static void main(String[] args) { + Graphic c1 = new Circle("圆形1",5); + System.out.println(c1.getInfo()); + + Graphic c2 = new Circle("圆形2",6); + System.out.println(c2.getInfo()); + + + Graphic r1 = new Rectangle("矩形1", 10,20); + System.out.println(r1.getInfo()); + + Graphic r2 = new Rectangle("矩形2",15,20); + System.out.println(r2.getInfo()); + System.out.println(); + + getArea(c1,c2); + getArea(c1,r1); + getArea(c2,r1); + getArea(c2,r2); + + System.out.println(); + + getPerimeter(c1,c2); + getPerimeter(c1,r1); + getPerimeter(c2,r1); + getPerimeter(c2,r2); + + } + + //判断面积大小 + public static void getArea(Graphic a,Graphic b){ + if(a.area()>b.area()){ + System.out.println(a.getName()+"的面积大于"+b.getName()); + }else{ + System.out.println(b.getName()+"的面积大于"+a.getName()); + } + + } + + //判断周长大小 + public static void getPerimeter(Graphic a ,Graphic b){ + if(a.perimeter()>b.perimeter()){ + System.out.println(a.getName()+"的周长大于"+b.getName()); + }else{ + System.out.println(b.getName()+"的周长大于"+a.getName()); + } + } +} + +~~~ + -- Gitee