簡體   English   中英

將.method放在對象之后會發生什么?

[英]What happens when you put a .method after an object?

我試圖了解為什么這將無法運行。 我認為我可能需要導入軟件包。 我還想知道myObject.f();行上的LocalVariables類中發生了什么;

我想我剛剛在上一行實例化了myObject,但是我是否要用myObject.f()調用f方法; ?????? 我不知道那條線上應該發生什么。
任何幫助,將不勝感激。

class MyObject{
 static short s = 400; //static variable
 int i = 200;  //instance variable

 void f() {
     System.out.println("s = " + s);
     System.out.println("i = " + i);
     short s = 300;  //local variable
     int i = 100;  //local variable
     double d = 1E100;  //local variable
     System.out.println("s = "+s);
     System.out.println("i = " +i);
     System.out.println("d = " + d);
          }
     }


class LocalVariables{
    public static void main(String[] args){
        MyObject myObject = new MyObject();
        myObject.f();
    }
}

是的,您正在調用f()方法。 因此,控制流將跳到f()函數的頂部,並逐一執行這些語句。

完成后,它將返回到main方法,恢復到上次停止的位置。 這並不意味着什么,因為myObject.f()是最后一行,但是如果您有更多代碼,則一旦f()方法返回,它將被執行。

執行得好,應該已經打印了幾行

s = 400
i = 200
s = 300
i = 100
d = <something>

這類內容是否出現在您的輸出中?

change void f() to public void f() { //Code... }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM