国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(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)方法的評論 (共 條)

分享到微博請遵守國家法律
固阳县| 宿迁市| 绥芬河市| 江山市| 同心县| 台南县| 新巴尔虎右旗| 龙江县| 柳林县| 大方县| 红桥区| 三江| 平度市| 股票| 绵阳市| 新乐市| 崇信县| 延安市| 通化县| 长寿区| 长汀县| 德昌县| 五台县| 上饶市| 拜城县| 昭平县| 徐汇区| 新河县| 深圳市| 钟祥市| 界首市| 乾安县| 呼玛县| 绩溪县| 师宗县| 余姚市| 德惠市| 偃师市| 泰安市| 南木林县| 长丰县|