Java static关键字的小练习

 1 package com.bytezreo.statictest;
 2 
 3 /**
 4  * 
 5  * @Description static 关键字的使用 小练习
 6  * @author Bytezero·zhenglei!        Email:420498246@qq.com
 7  * @version
 8  * @date 下午12:51:45
 9  * @
10  *
11  */
12 public class CircleTest 
13 {
14     public static void main(String[] args) 
15     {
16         Circle c1 = new Circle();
17         
18         Circle c2 = new Circle();
19         
20         Circle c3 = new Circle();
21         System.out.println("c1的id:"+c1.getId());
22         System.out.println("c2的id:"+c2.getId());
23         System.out.println("c3的id:"+c3.getId());
24         
25         System.out.println("创建圆的个数为:"+Circle.getTotal());
26         
27         
28     }
29 }
30 
31 class Circle
32 {
33     private double radius;
34     private int id;     //自动赋值
35     
36     //构造器
37     public Circle()
38     {
39         id = init++;
40         total++;
41     }
42     
43     public Circle(double radius)
44     {
45         this();
46 //        id = init++;        //自动累加id
47 //        total++;
48         
49         this.radius = radius;
50         
51     }
52     
53     
54 
55     private static int total;   //记录创建圆的个数
56     private static int init = 1001;     //静态 多个对象共享
57     
58     public double findArea()
59     {
60         return 3.14 * radius * radius;
61     }
62 
63     public double getRadius() {
64         return radius;
65     }
66 
67     public void setRadius(double radius) {
68         this.radius = radius;
69     }
70 
71     public int getId() {
72         return id;
73     }
74 
75     public static int getTotal() {
76         return total;
77     }
78 
79 
80 
81     
82     
83     
84     
85 }

Java   static关键字的小练习

 

上一篇:认识Circle数据库


下一篇:根据下图实现类。在CylinderTest类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积,继承性