• 类方法/静态方法


    类方法

    类方法的定义

    类方法也叫静态方法
    定义形式:

    • 访问修饰符 static 返回类型 方法名(形参列表){}推荐使用
    • static 访问修饰符 返回类型 方法名(形参列表){}

    类方法的使用

    同类变量一样:可以使用类名.类方法名调用,也可以对象名.类方法名调用(需要遵守访问修饰符规则)

    案例了解:

    public class StaticMethod {
        public static void main(String[] args) {
            Stu xm = new Stu("小明");
            xm.payFee(200);
            Stu xh = new Stu("小红");
            xh.payFee(200);
            Stu.showFee();
        }
    }
    class Stu{
        private static double fee;//类变量
        String name;
        public Stu(String name) {
            this.name = name;
        }
        public static void payFee(double fee){//类方法
            Stu.fee+=fee;
        }
        public static void showFee(){//类方法
            System.out.println(Stu.fee);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    输出结果:400

    类方法的使用场景

    • 当方法中不涉及任何和对象相关的成员,就可以将方法设计成静态方法。
      或者将一些工具方法,设计成静态方法。例如:打印一维数组,冒泡排序。。。。

    就像要求两个数的和,如果这个方法设计成实例方法,那么每创建一个对象,就要浪费内存去存放这个方法,而将它设计出静态方法时,就不会每次实例化对象都要一点内存。

    类方法使用细节

    1. 类方法和普通方法都是随着类加载而加载的,将结构信息存储在方法区中
    2. 类方法可以通过类名调用,也可以通过对象名调用
    3. 普通方法和对象有关,需要通过对象名调用,不能通过类名调用
    4. 类方法中不允许使用和对象有关的关键字,比如this,super。普通方法可以使用
    5. 类方法中只能访问静态成员,普通方法都可以方法。也就是静态只能访问静态,非静态不管是不是静态都可以访问。

    类变量和类方法练习

    1.看看输出的是什么?

    publi class Test{
    	static int count = 9 ;
    	public void count(){
    	System.out.println(count++)
    	}
    	public static void main(String[]args){
    		new Test.count();
    		new Test.count();
    		System.out.println(count)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    输出结果: 9 10 11(后++,先运算 后自增)

    2.查找出下面代码的错误。且修改,并看看输出结果

    public class Person {
        private int id ;
        private static  int total;
        public static int getTotal(){
            //id++;静态只能调用静态
            return total;
        }
        public Person(){
            total++;
            id = total;
        }
    }
    class testPerson{
        public static void main(String[] args) {
            System.out.println(Person.getTotal());//输出 0 
            Person p1 = new Person();//使用构造器,+1
            System.out.println(Person.getTotal());//输出 1
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3.查找出下面代码的错误。且修改,并看看最后total的值是多少

    public class Person {
        private int id ;
        private static  int total;
        public static void setTotal(int total){
            //this.total = total,静态方法不能使用有关对象的关键字
            Person.total = total;
        }
        public Person(){
            total++;
            id = total;
        }
    }
    class testPerson{
        public static void main(String[] args) {
            Person.setTotal(3);
            new Person();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    最后total的值是 4

  • 相关阅读:
    哈工大李治军老师操作系统笔记【7】:多进程图像(Learning OS Concepts By Coding Them !)
    英国开发者年龄歧视为29岁,女程序员幸福指数略高于男性 | 全球开发者幸福指数报告
    (09_13)杭州站|阿里云 Serverless 技术实践营(Serverless + 大数据)开启报名!
    用java代码实现QQ第三方登录
    ON CONFLICT语句
    项目开发流程
    2023年中国复合门产量、销量、产业链及市场规模分析[图]
    zotero跨Windows设备数据同步(利用OneDrive、百度云同步空间等云服务)
    2023年10月12日
    单元测试编写规范
  • 原文地址:https://blog.csdn.net/WINorYU/article/details/127600487