1. package com.atguigu.exercise1;
    2. //static关键字的应用
    3. public class CircleTest {
    4. public static void main(String[] args) {
    5. Circle c1 = new Circle();
    6. Circle c2 = new Circle();
    7. Circle c3 = new Circle(3.4);
    8. System.out.println("c1的id:"+ c1.getId());
    9. System.out.println("c2的id:"+ c2.getId());
    10. System.out.println("c3的id:"+ c3.getId());
    11. System.out.println("创建的圆的个数为:"+ Circle.getTotal());
    12. }
    13. }
    14. class Circle{
    15. private double radius;
    16. private int id;
    17. public Circle(){
    18. id = init++;
    19. total++;
    20. }
    21. public Circle(double radius){
    22. this();
    23. // id = init++;
    24. // total++;
    25. this.radius = radius;
    26. }
    27. private static int total;
    28. private static int init = 1001;//static声明的属性被所有的对象所共享
    29. public double findArea(){
    30. return Math.PI*radius*radius;
    31. }
    32. public double getRadius() {
    33. return radius;
    34. }
    35. public void setRadius(double radius) {
    36. this.radius = radius;
    37. }
    38. public int getId() {
    39. return id;
    40. }
    41. public void setId(int id) {
    42. this.id = id;
    43. }
    44. public static int getTotal() {
    45. return total;
    46. }
    47. }