29 面向对象编程 static 关键字
生活随笔
收集整理的這篇文章主要介紹了
29 面向对象编程 static 关键字
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
補充:static
代碼
// static
public class Student{
private static int age; // 靜態的變量 多線程
private double score; // 非靜態的比那輛
public void run(){
}
public static void go(){
}
// main 方法
{
go();
//-------
Student s1 = new Student();
System.out.println(Student.age); // 類變量
System.out.println(s1.age); // 對象的變量
System.out.println(s1.score);
}
}
// ----------------------
public class Person(){
{
// 創建這個對象的時候就創建了,在構造器之前
// 沒有名字,程序不能調用這個模塊
// 代碼塊(匿名代碼塊)
}
static{
// 靜態代碼塊
// 跟類一加載,就執行,只執行一次
}
}
// ==============================
public class Person(){
{
// 賦初始值
System.out.println("匿名代碼塊"); //執行順序:2
}
static{
// 只執行一次
System.out.println("靜態代碼塊"); //執行順序:1
}
public Person(){
System.out.println("構造方法"); //執行順序:3
}
}
// 靜態導入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test(){
System.out.println(random());
System.out.println(PI);
}
總結
以上是生活随笔為你收集整理的29 面向对象编程 static 关键字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: April 9 2017 Week 15
- 下一篇: HDU 4622 Reincarnati