0

我正在使用 RIOT Games APi 并使用提供的示例代码,但控制台显示:

net.rithms.riot.dto.Game.RecentGames@35d176f7

我不太确定,我写了一个不同的代码来请求用户 ID,它工作得很好。

import net.rithms.riot.constant.Region;
import net.rithms.riot.constant.Season;
import net.rithms.riot.api.RiotApi;
import net.rithms.riot.api.RiotApiException;
import net.rithms.riot.dto.Game.RecentGames;

public class Example {

public static void main(String[] args) throws RiotApiException {

        RiotApi api = new RiotApi("KEY", Region.EUW);
        api.setSeason(Season.CURRENT);

        RecentGames recentGames = api.getRecentGames(api.getSummonerByName("Vodkamir Putkin").getId());

        System.out.println(recentGames);
    }
}

不确定这意味着什么或如何处理它,根据 API,它应该显示有关我最近的游戏的信息

4

1 回答 1

1

System.out.println(recentGames);

这将隐式调用对象toString()上的方法recentGames。除非RecentGames该类覆盖该toString()方法,否则根据上面链接的文档,它将有效地打印:

getClass().getName() + '@' + Integer.toHexString(hashCode())

我不熟悉 RIOT API,但如果你想获得更具体的信息,最好的办法是看看你可以在RecentGames对象上调用哪些其他方法。


编辑:

只要您继续调用返回不覆盖对象的方法toString(),您就会一直遇到同样的问题。

System.out是一个PrintStream对象。花一些时间查看文档,特别是print(...)println(...)方法。

例如,如果你传入的是一个 的东西int,你正在调用print(int)orprintln(int)方法。如果传入 a String,则调用print(String)orprintln(String)方法。如果你传入一个原语,你正在调用相应的方法。如果你传入任何 other Object,你实际上是在这样做:

Object myObject;
String myObjectAsAString = myObject.toString(); // See above for what this evaluates to
                                                // if the class doesn't override toString()
System.out.println(myObjectAsAString);

如果你真的想打印出有意义的信息,你有两种选择:

  1. 保持对对象的调用方法,直到您希望获得一个类,该类具有返回String可以打印或覆盖的原语或原语的方法toString()
  2. 编写一些逻辑来解释您正在调用的方法的结果。例如,代替System.out.println(recentGames.getGames());,您可以检查 ifrecentGames.getGames().isEmpty()和 print No recent games,或类似的东西。
于 2017-05-04T20:24:44.720 回答