五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊

static 靜態(tài)變量和靜態(tài)方法

2022-06-14 13:05 作者:虛云幻仙  | 我要投稿

/**
* 測試static 靜態(tài)變量和靜態(tài)方法 隨類加載到方法區(qū)內(nèi)
* 靜態(tài)初始化塊
*/

public class TestStatic {
? ?int id;
? ?String name;
? ?static String country = "China";
? ?//static變量 靜態(tài)變量屬于類 不屬于對象 new的對象不具有country屬性


? ?public TestStatic(int id, String name) {
? ? ? ?this.id = id;
? ? ? ?this.name = name;
? ? ? ?//右鍵generate constructor 生成構(gòu)造方法
? ?}

? ?void aaa(){}
? ?public static void where(){
? ? ? ?//void 無返回值
? ? ? ?System.out.println(country);
? ? ? ?//static方法內(nèi)可以調(diào)用static變量或static方法
? ? ? ?//static方法屬于類,類方法調(diào)用類對象不用再定義country直接使用
? ? ? ?//普通方法aaa屬于對象,類方法無法調(diào)用
? ? ? ?//this屬于對象,類方法也無法調(diào)用

? ?}

? ?public static void main(String[] args) {
? ? ? ?TestStatic u1 = new TestStatic(01,"li");
? ? ? ?TestStatic.where();
? ? ? ?//調(diào)用static方法即類方法,輸入類名.方法名()
? ? ? ?u1.aaa();
? ? ? ?//u1指向?qū)ο笳{(diào)用aaa方法,對象.方法名()
? ? ? ?TestStatic.country = "CN";
? ? ? ?//靜態(tài)變量可修改
? ? ? ?TestStatic.where();
? ?}
}

class TestStatic2{
? ?static String country;
? ?static {
? ? ? ?//語句塊外加static 靜態(tài)初始化塊 在類加載時(shí)執(zhí)行
? ? ? ?System.out.println("類的初始化操作執(zhí)行中");
? ? ? ?country = "China";
? ? ? ?//調(diào)用類變量country
? ? ? ?where();
? ? ? ?//調(diào)用類方法where
? ?}
? ?public static void where(){
? ? ? ?System.out.println(country);
? ?}

? ?public static void main(String[] args) {
? ? ? ?//main方法空
? ?}
}

static 靜態(tài)變量和靜態(tài)方法的評論 (共 條)

分享到微博請遵守國家法律
西城区| 通辽市| 安达市| 南宁市| 公安县| 济阳县| 建德市| 油尖旺区| 古丈县| 鄂尔多斯市| 庆城县| 微山县| 青铜峡市| 扬州市| 岳池县| 老河口市| 昌平区| 疏附县| 清水县| 温泉县| 台江县| 修文县| 东明县| 巴东县| 高平市| 若尔盖县| 白沙| 白朗县| 即墨市| 景谷| 阳曲县| 浑源县| 琼中| 贵定县| 平阴县| 安福县| 鞍山市| 大足县| 巫山县| 兴隆县| 泗阳县|