zoukankan      html  css  js  c++  java
  • static关键字的应用

    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 + "]";
        }
    }
    All that work will definitely pay off
  • 相关阅读:
    素材收集
    网站返回503
    uva 1048 最短路的建图 (巧,精品)
    hdu5188 01 背包
    hdu 5187 快速幂 + 快速乘 值得学习
    差分约束
    uva11090 Bellman-Ford 运用
    hdu 5185 动态规划 分析降低复杂度
    hdu5184 数论证明
    HDU5183 hash 表
  • 原文地址:https://www.cnblogs.com/afangfang/p/12526474.html
Copyright © 2011-2022 走看看