每日代码系列(10)

  • 2020 年 12 月 7 日
  • 筆記
 1 class Cylinder {
 2   private double radius;
 3   private int height;
 4   private double pi=3.14;
 5   String color;
 6   public Cylinder() {
 7     this(2.5,5,"红色");
 8     System.out.println("无参构造方法被调用了");
 9   }
10   public Cylinder(double r,int h,String str) {
11     System.out.println("有参构造方法被调用了");
12     radius=r; height=h;color=str;
13   }
14   double area() {
15     return pi*radius*radius;
16   }
17   double volume() {
18     return area()*height;
19   }
20 }
21 public class C1_6 {
22   public static void main(String[] args) {
23     Cylinder volu=new Cylinder();
24     System.out.println("底面积为: "+volu.area());
25     System.out.println("体积为: "+volu.volume());
26   }
27 }