每日程式碼系列(15)

李家宇發表於2020-12-12
 1 abstract class Shape {
 2   public abstract float area();
 3   public abstract void printArea();
 4 }
 5 class Rectangle extends Shape {
 6   int width;
 7   int length;
 8   public Rectangle(int newWidth,int newLength) {
 9     width=newWidth;
10     length=newLength;
11   }
12   public float area() {
13     float area=width*length;
14     return area;
15   }
16   public void printArea() {
17     System.out.println("矩形的面積: "+area());
18   }
19 }
20 class Circle extends Shape {
21   final float PI=3.14F;
22   int radius;
23   public Circle(int newRadius) {
24     radius=newRadius;
25   }
26   public float area() {
27     float area=PI*radius*radius;
28     return area;
29   }
30   public void printArea() {
31     System.out.println("圓的面積: "+area());
32   }
33 }
34 class ChouXiang {
35   public static void main(String[] args) {
36     Rectangle s1=new Rectangle(3,4);
37     Circle s2=new Circle(2);
38     s1.printArea();
39     s2.printArea();
40   }
41 }

有什麼意見,下面評論區說哈。

相關文章