第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

不創(chuàng)建show()方法,為什么也是可行的?不創(chuàng)建和創(chuàng)建有什么區(qū)別?

public class HelloWorld {

? ??

? ? String name; // 聲明變量name

String sex; //?聲明變量sex

static int age;// 聲明靜態(tài)變量age

? ??

? ? // 構(gòu)造方法

public ? HelloWorld ? ? () {?

System.out.println("通過構(gòu)造方法初始化name");

name = "tom";

}

? ??

? ? // 初始化塊

{?

System.out.println("通過初始化塊初始化sex");

sex = "男";

}

? ??

? ? // 靜態(tài)初始化塊

? ?static ? ?{?

System.out.println("通過靜態(tài)初始化塊初始化age");

age = 20;

}

? ??


? ??

public static void main(String[] args) {

? ? ? ??

? ? ? ? // 創(chuàng)建對象

HelloWorld hello = new HelloWorld();

// 調(diào)用對象的show方法

? ? ? ? System.out.println("姓名:"+hello.name+",性別:"+hello.sex+",年齡:"+age);

}

}


正在回答

1 回答

不創(chuàng)建show()方法直接println是通過對象.屬性名直接調(diào)用你在HelloWorld類中的屬性(已賦值);

創(chuàng)建了show()方法是通過對象名.方法名調(diào)用里面的方法,相對于來說,創(chuàng)建方法比調(diào)用屬性更加方便,看起來也更加明了。

希望能幫助到你!

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

qq_失落的世界_3 提問者

非常感謝!
2016-11-30 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

不創(chuàng)建show()方法,為什么也是可行的?不創(chuàng)建和創(chuàng)建有什么區(qū)別?

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號