12 changed files with 58 additions and 0 deletions
@ -0,0 +1,2 @@ |
|||||
|
利用AI生成并学习代码,在写玩代码后,用AI进行润色。 |
||||
|
对与不熟悉的概念AI帮忙解答。 |
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,56 @@ |
|||||
|
package w5; |
||||
|
|
||||
|
public class ShapeCalculator { |
||||
|
abstract class Shape { |
||||
|
public abstract double getArea(); |
||||
|
} |
||||
|
class Circle extends Shape { |
||||
|
private double radius; |
||||
|
public Circle(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return Math.PI * radius * radius; |
||||
|
} |
||||
|
} |
||||
|
class Rectangle extends Shape { |
||||
|
private double width; |
||||
|
private double height; |
||||
|
public Rectangle(double width, double height) { |
||||
|
this.width = width; |
||||
|
this.height = height; |
||||
|
} |
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return width * height; |
||||
|
} |
||||
|
} |
||||
|
class Triangle extends Shape { |
||||
|
private double base; |
||||
|
private double height; |
||||
|
public Triangle(double base, double height) { |
||||
|
this.base = base; |
||||
|
this.height = height; |
||||
|
} |
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return 0.5 * base * height; |
||||
|
} |
||||
|
} |
||||
|
class ShapeUtil { |
||||
|
public static void printArea(Shape shape) { |
||||
|
System.out.println("图形面积为:" + shape.getArea()); |
||||
|
} |
||||
|
} |
||||
|
public static void main(String[] args) { |
||||
|
ShapeCalculator calculator = new ShapeCalculator(); |
||||
|
Shape circle = calculator.new Circle(5); |
||||
|
Shape rectangle = calculator.new Rectangle(4,6); |
||||
|
Shape triangle = calculator.new Triangle(3,8); |
||||
|
System.out.println("====== 图形面积计算器 ======"); |
||||
|
ShapeUtil.printArea(circle); |
||||
|
ShapeUtil.printArea(rectangle); |
||||
|
ShapeUtil.printArea(triangle); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 72 KiB |
Binary file not shown.
Loading…
Reference in new issue