簡體   English   中英

Java從方法返回

[英]Java return from a method

打擾一下,這可能是一個非常明顯的問題,但實際上我整天都在從事一個項目的工作,現在我陷入了一個非常簡單的問題,但似乎無法解決。 所以我有這個非常簡單的方法

public String method(final Node<Tag> child) {
    return child.getData().getAttributeValue();
} 

我相信它返回一個字符串,現在我的問題是如何獲取該返回字符串並在另一個方法中打印它。 換句話說,我想打印此方法返回的內容。 非常感謝您的寶貴時間!

編輯:我嘗試使用下面的兩個選項調用該方法,並且出現以下錯誤:

BrowserGui.java:185: error: cannot find symbol String foo = method(childNode); ^ symbol: variable childNode

並簡單地稱之為:

BrowserGui.java:195: error: cannot find symbol
                   System.out.print(child.getData().getAttributeValue());
                                    ^
symbol:   variable child
location: class BrowserGui.TextClickListener
1 error

只需調用sysout即可

System.out.println(child.getData().getAttributeValue());

相當於

String value = child.getData().getAttributeValue();
System.out.println(value);

將返回值分配給變量並打印

String foo = method(childNode);
System.out.println(foo);

或者直接使用System.out.println(method(childNode));顯示返回值System.out.println(method(childNode));

暫無
暫無

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

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