static關鍵字的使用

2021-07-15 19:02:59 字數 1056 閱讀 3137

package com.liaojianya.chapter2;

/** * static關鍵字的使用

* @author liao jianya

* 2023年7月28日

*/public class staticdemo

}class people

public string talk()

public static void print()

}

執行結果:

before modifying the information: my name is zhangsan, i'm 20years old, i'm from nanjing

before modifying the information: my name is lisi, i'm 21years old, i'm from nanjing

before modifying the information: my name is wangwu, i'm 22years old, i'm from nanjing

*************after modifying the information************

after modifying the information: my name is zhangsan, i'm 20years old, i'm from the usa

after modifying the information: my name is lisi, i'm 21years old, i'm from the usa

after modifying the information: my name is wangwu, i'm 22years old, i'm from the usa

產生了:3 個例項物件

分析:

1)static宣告的屬性city是所有物件共享的,所以,p1呼叫的屬性city改變其值,全部物件的city都發生了同樣的變化。

2)使用static修飾的屬性i,加入到構造方法中去計算類產生的例項物件。

使用static關鍵字

在前面的課程中,你學到了區域性變數 有塊範圍 和全域性變數 有計畫的範圍 還有另乙個作用域的變數,可以水平 檔案範圍。檔案範圍內的變數可以被訪問的任何功能或嵌在乙個單一的檔案。申報檔案作用域的變數,簡單地宣告乙個變數,一塊外 同乙個全域性變數 但使用static關鍵字 123 4567 static...

使用static關鍵字

在前面的課程中,你學到了區域性變數 有塊範圍 和全域性變數 有計畫的範圍 還有另乙個作用域的變數,可以水平 檔案範圍。檔案範圍內的變數可以被訪問的任何功能或嵌在乙個單一的檔案。申報檔案作用域的變數,簡單地宣告乙個變數,一塊外 同乙個全域性變數 但使用static關鍵字 123 4567 static...

static關鍵字使用

版本 v1.0 public class study 6 1 class person4 靜態使用注意事項 1.靜態方法只能訪問靜態成員 非靜態的方法既可以訪問靜態也可以訪問非靜態 這點注意先後順序就可以理解 2.靜態方法中不可以定義this,super等關鍵字 因為靜態優先於物件存在。所以靜態方法...