每日代码系列(9)

 1 class Cylinder {
 2   private double radius;
 3   private int height;
 4   private double pi=3.14;
 5   private String color;
 6   public double setCylinder(double r,int h) {
 7     radius=r;
 8     height=h;
 9     return r+h;
10   }
11   public void setCylinder(String str) {
12     color=str;
13   }
14   public void show() {
15     System.out.println("颜色为:"+color);
16   }
17   double area() {
18     return pi*radius*radius;
19   }
20   double volume() {
21     return area()*height;
22   }
23 }
24 public class C1_1 {
25   public static void main(String[] args) {
26     double r_h;
27     Cylinder volu=new Cylinder();
28     r_h=volu.setCylinder(2.5,5);
29     volu.setCylinder("红色");
30     System.out.println("半径和高之和="+r_h);
31     System.out.println("体积="+volu.volume());
32     volu.show();
33   }
34 }

 

posted @ 2020-12-04 13:37  李家宇  阅读(97)  评论(0编辑  收藏  举报