• static详解


    1

    package com.mypackage.oop.demo10;
    
    public class Student10 {
        public static int age;   //静态变量
        private double score;   //非静态变量
    
        public void run(){
            System.out.println("run");
        }
    
        public static void go(){
            System.out.println("go");
        }
    
    
        public static void main(String[] args) {
            Student10 student10 = new Student10();
    
            System.out.println(Student10.age);    //类变量
            //System.out.println(Student10.score);  //报错
            System.out.println(student10.age);    //下面这两个是和对象有
            System.out.println(student10.score);   //非静态,不能直接调用,必须new了才能用
    
            //run();    不能这样直接调用
            new Student10().run();    //非静态,不能直接调用,必须new了才能用
            go();   //静态,可以直接调用
    
            //静态的才能直接被调用,非静态的不能直接被调用
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    2

    package com.mypackage.oop.demo10;
    
    public class Person {
        //匿名代码块
        {
            System.out.println("匿名代码块");
        }
    
        //静态代码块
        static {
            System.out.println("静态代码块");
        }
    
        //构造方法
        public Person() {
            System.out.println("构造方法");
        }
    
        public static void main(String[] args) {
            Person person1 = new Person();
            //输出结果及顺序为:静态代码块,匿名代码块,构造方法
            System.out.println("==============");
    
            Person person2 = new Person();
            //输出结果及顺序为:匿名代码块,构造方法
    
            //静态方法只执行一次
            //匿名代码块可以用来赋初始值
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    3

    package com.mypackage.oop.demo10;
    
    import static java.lang.Math.random;    //静态导入包
    import static java.lang.Math.PI;
    
    public class Test {
        public static void main(String[] args) {
            //random  一种产生随机数的方法
            System.out.println(Math.random());   //没导入包之前,前面必须要加个Math
            System.out.println(random());     //导入包之后,Math可加可不加
            System.out.println(PI);          //输出3.141592653589793
        }
    }
    
    //被final修饰的类不能被继承,断子绝孙!!!
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    (pytorch进阶之路)U-Net图像分割
    leecode #加一#二进制求和
    字典树原理与实现
    MQ 介绍
    JS之同步异步promise、async、await
    【新书推荐】人工智能的当下,测试团队如何敏捷转型 —— 无测试组织
    云计算(Docker)
    年轻人开发谁用默认背景?我直接美图安排上
    小程序+音视频1:live-pusher
    某光伏行业头部企业联合谢宁老师《向华为学习 业务领先的战略规划SP(BLM)和战略解码BP(BEM)》训战内训成功举办
  • 原文地址:https://blog.csdn.net/hxh_230810/article/details/133217521