1. 程式人生 > 遊戲攻略 >《前往中世紀》實用技巧分享

《前往中世紀》實用技巧分享

package com.oop.demo05;

/**
 * <p>
 *
 * </p>
 *
 * @author: wfs
 * @date: 2021/6/21
 */
public class Student {
    private  static  int age;//靜態的變數  多執行緒
    private  double score;//非靜態
    public void run(){

    }
    public static void go(){

    }

    public static void main(String[] args) {
        System.out.println(Student.age);
        
//System.out.println(Student.score);沒有static修飾 Student s1=new Student(); System.out.println(s1.age); System.out.println(s1.score); } }
package com.oop.demo05;

/**
 * <p>
 *
 * </p>
 *
 * @author: wfs
 * @date: 2021/6/21
 */
public class Student {
    private  static
int age;//靜態的變數 多執行緒 private double score;//非靜態 public void run(){ go();//非靜態可以訪問靜態方法所有東西 } public static void go(){ //靜態方法可以呼叫靜態方法,不可以呼叫非靜態方法 } public static void main(String[] args) { new Student().run(); Student.go(); go();//非靜態的方法可以訪問main
//run();//靜態方法可以呼叫靜態方法,不可以呼叫非靜態方法 } }

靜態程式碼塊 &非靜態程式碼塊

package com.oop.demo05;

/**
 * <p>
 *
 * </p>
 *
 * @author: wfs
 * @date: 2021/6/21
 */
public class Person {
    {
        System.out.println("匿名程式碼塊");
    }
    static {//只執行一次
        System.out.println("靜態程式碼塊");
    }
    Person(){
        System.out.println("無參構造");
    }

    public static void main(String[] args) {
        Person person = new Person();

    }


}

執行的結果

靜態程式碼塊 匿名程式碼塊 無參構造

 public static void main(String[] args) {
        Person person = new Person();
        System.out.println("============");
        Person person1 = new Person();

    }
靜態程式碼塊
匿名程式碼塊
無參構造
============
匿名程式碼塊
無參構造
package com.oop.demo05;
import static java.lang.Math.random;
//匯入靜態程式碼包
/**
 * <p>
 *
 * </p>
 *
 * @author: wfs
 * @date: 2021/6/21
 */
public class Test {
    public static void main(String[] args) {
        System.out.println(random());

    }
}