首页 > 其他 > 详细

static关键字的应用

时间:2020-03-19 19:15:19      阅读:33      评论:0      收藏:0      [点我收藏+]

static关键字的应用:使用静态的变量可以实现   "累加" 的效果

package com.aff.statics;

public class TestCircle {
    public static void main(String[] args) {
        Circle c1 = new Circle(2.0);
        Circle c2 = new Circle(2.2);
        System.out.println(Circle.getTotal());
        Circle.show();
        c1.setInfo("小圆-->效原");
        Circle.show();
        c2.show();
    }
}

class Circle {
    private double radius;// 半径
    private static String info = "效原";
    private static int total = 0;// 因为total是static的,在内存中独一份,所以可以用来记录创建的对象的个数

    public Circle(double radius) {
        super();
        this.radius = radius;
        total++;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public static String getInfo() {
        return info;
    }

    public static void setInfo(String info) {
        Circle.info = info;
    }

    public static int getTotal() {
        return total;
    }

    public static void setTotal(int total) {
        Circle.total = total;
    }

    public static void show() {
        System.out.println(info);
    }

    @Override
    public String toString() {
        return "Circle [radius=" + radius + "]";
    }
}

 

static关键字的应用

原文:https://www.cnblogs.com/afangfang/p/12526474.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!