全网最细之static关键字讲解
生活随笔
收集整理的這篇文章主要介紹了
全网最细之static关键字讲解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.wuming.oop.demo07;public class Person {//2:賦初值{System.out.println("匿名代碼塊");}//1:只執行一次static{System.out.println("靜態代碼塊");}//3public Person() {System.out.println("構造方法");}public static void main(String[] args) {Person person1 = new Person();//上一行執行完,輸出順序// 靜態代碼塊//匿名代碼塊//構造方法System.out.println("=============");Person person2 = new Person();/*上一行執行完,輸出順序* 匿名代碼塊構造方法* */}
}
package com.wuming.oop.demo07;
//static
public class Student{private static int age;//靜態的變量 多線程!private double score;//非靜態的變量public void run(){go();}public static void go(){//是靜態方法, Student.go();s1.go(); go();調用ok,非靜態方法可以調用靜態方法第7行//run();靜態方法不能調用非靜態方法}public static void main(String[] args) {Student s1= new Student();System.out.println(Student.age);// System.out.println(Student.score);非靜態的變量,不能用類名直接調用System.out.println(s1.age);System.out.println(s1.score);Student.go();s1.go();go();// run();不能直接調用非靜態方法new Student().run();}}
package com.wuming.oop.demo07;//靜態導入包
import static java.lang.Math.random;//加static,拓展:Math是被final修飾的,凡是被final修飾的類不能被繼承
import static java.lang.Math.PI;
public class Test {public static void main(String[] args) {System.out.println(random());System.out.println(Math.random());//沒有導包就這樣寫System.out.println(PI);}
}
總結
以上是生活随笔為你收集整理的全网最细之static关键字讲解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python sys模块 - Pytho
- 下一篇: C语言 野指针 - C语言零基础入门教程