首页 > 其他 > 详细

静态变量 静态方法

时间:2017-02-20 13:22:40      阅读:171      评论:0      收藏:0      [点我收藏+]

/*
作者:qingfeng
日期:2017/2/20
功能:类变量(静态变量),类方法(静态方法) 统计学费总和
*/

public class Demo3_4
{

    public static void main(String args[]){
        Stu s1=new Stu(23,"qingfeng",200);
        Stu s2=new Stu(20,"lele",123);
        System.out.println(Stu.getTotalFee());
    }
}

class Stu
{
    int age;
    String name;
    int fee;
    static int totalFee;

    public Stu(int age, String name, int fee){
        this.age = age;
        this.name = name;
        totalFee +=fee;
    }
    //类方法(静态方法):用static所有对象共享一个方法,节省栈的开销
    //静态方法不能访问非静态变量
    //java规则:静态变量原则上用静态方法去访问和操作
    public static int getTotalFee(){
        return totalFee;
    }
}


静态变量 静态方法

原文:http://www.cnblogs.com/qingfengzhuimeng/p/6418548.html

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