亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

最后的pri2 hello2=new pri2()是怎么運行的?

package com.imooc;

public class pri2{
?? ?int num1;
?? ?int num2;
?? ?static int num3;
?? ?public pri2(){
?? ??? ?num1=91;
?? ??? ?System.out.println("通過構造方法為變量num1賦值");
?? ??? ?}
?? ??? ?{
?? ??? ??? ?num2=74;
?? ??? ??? ?System.out.println("通過初始化模塊為num2賦值");
?? ??? ?}
?? ??? ?static{
?? ??? ??? ?num3=83;
?? ??? ??? ?System.out.println("通過靜態初始化模塊為怒目賦值");
?? ??? ?}
?? ??? ?public static void main(String[] args) {
?? ??? ??? ?pri2 hello=new pri2();
?? ??? ??? ?System.out.println("num1:"+hello.num1);
?? ??? ??? ?System.out.println("num2:"+hello.num2);
?? ??? ??? ?System.out.println("num3:"+num3);
?? ??? ??? ?pri2 hello2=new pri2();
?? ??? ?}
?? ?}

正在回答

2 回答

pri2 hello=new pri2();
?? ??? ??? ?System.out.println("num1:"+hello.num1);
?? ??? ??? ?System.out.println("num2:"+hello.num2);
?? ??? ??? ?System.out.println("num3:"+num3);
?? ??? ??? ?pri2 hello2=new pri2();

這些代碼等同于

pri2 hello=new pri2();

?pri2 hello2=new pri2();

?? ??? ??? ?System.out.println("num1:"+hello.num1);
?? ??? ??? ?System.out.println("num2:"+hello.num2);
?? ??? ??? ?System.out.println("num3:"+num3);

num3屬于靜態類只能被執行一次,所以執行結果缺少“”通過靜態初始化塊初始化? num3“ ? ? ? ??

0 回復 有任何疑惑可以回復我~

Static修飾的類成員,它的使用完全獨立于該類的任何對象。Static塊僅在該類被加載時執行一次。

0 回復 有任何疑惑可以回復我~
#1

Chen十八 提問者

非常感謝!
2017-04-22 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

最后的pri2 hello2=new pri2()是怎么運行的?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號