Browse Source

feat:w4

main
WangJunyue 2 weeks ago
parent
commit
59e76ecfeb
  1. 39
      w4/Shape.java
  2. 11
      w4/ShapeUtil.java
  3. BIN
      w4/实验报告.docx
  4. BIN
      w4/类图.jpg

39
w4/Shape.java

@ -0,0 +1,39 @@
package learn;
public 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 length;
private double width;
public Rectangle(double length,double width){
this.length=length;
this.width=width;
}
@Override
public double getArea(){
return length*width;
}
}
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 base*height/2;
}
}

11
w4/ShapeUtil.java

@ -0,0 +1,11 @@
package learn;
public class ShapeUtil {
public static void printArea(Shape shape){
System.out.println(shape.getArea());
}
public static void main(String[]args){
Shape circle=new Circle(5);
printArea(circle);
}
}

BIN
w4/实验报告.docx

Binary file not shown.

BIN
w4/类图.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Loading…
Cancel
Save