简体   繁体   English

Java:如何在另一个类中使用一个类的toString()方法?

[英]Java: How to use the toString() method of one class in an other one?

I'm working on this project while learning Java, I created a Card class that has a toString() method but when I'm trying to convert a Card object to a String in the Deck class I'm getting an error . 我在学习Java时正在从事这个项目,我创建了一个具有toString()方法的Card类,但是当我尝试在Deck类中将Card对象转换为String时,却遇到了错误

Here is the Card class: 这是Card类:

private final Rank cardRank;
private final Suit cardSuit;

public Card(Rank cardRank, Suit cardSuit){
    this.cardRank = cardRank;
    this.cardSuit = cardSuit;
}

public String toString(){
    return "The " + cardRank + " of " + cardSuit;
}

Here is the Deck class: 这是Deck类:

private Card[] card = new Card[52];

public Deck(){
    int i = 0;
    for(Suit suit: Suit.values()){
        for(Rank rank: Rank.values()){
            card[i++] = (new Card(rank, suit));
        }
    }
}

public String toString(int i){
    return card[i];
}    

You can't cast a Card object into String object. 您不能将Card对象转换为String对象。

You can get the String representation of the Card object through toString() method. 您可以通过toString()方法获取Card对象的String表示形式。

Try like below 尝试如下

public String toString(int i){
    return card[i].toString();
} 

Calling return card[i] returns an object of type card. 调用返回卡[i]返回卡类型的对象。 You have to actually call the toString() method so that your return object is a String and not a Card. 您必须实际调用toString()方法,以便您的返回对象是String而不是Card。 Hope this helps! 希望这可以帮助!

If the toString() of each of your class does not have the @Override annotation, then you have to explicitly call the toString() . 如果每个类的toString()没有@Override批注,则必须显式调用toString()

Example: 例:

return card[i].toString();
return "The " + cardRank.toString() + " of " + cardSuit.toString;

If you add the @Override annotation like this: 如果您像这样添加@Override注释:

@Override
public String toString(){
    return "The " + cardRank.toString() + " of " + cardSuit.toString();
}

Then the Deck class toString(int i) can look like: 然后,Deck类toString(int i)可以看起来像:

public String toString(int i){
    return card[i];
} 

If you add the @Override to the Rank and Suit class, then the Card class toString() can look like: 如果将@Override添加到Rank and Suit类,则CardtoString()可能类似于:

public String toString(){
    return "The " + cardRank + " of " + cardSuit;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CodeHS:如何在另一个类的 toString 中多次使用一个类的 toString 方法 - CodeHS: How use one class's toString method in another class's toString multiple times 如何在TestNG中将一类方法转换为另一类 - How to use method of one class into other in TestNG java中如何在没有main方法的情况下从一个类调用toString()方法到另一个类 - How to call toString() Method from one class to another class without main method in java 在toString方法Java中调用其他类时遇到麻烦 - Trouble calling other class in toString method Java 来自其他类的方法和 toString() 的使用 - Method from other class and use of toString() 如何使用Java中的toString方法? - How to use the toString method in Java? 一个人如何通过继承将此toString方法用于私有变量? - How would one use this toString method for private variables via inheritance? 如何在Java中的同一个类中为多个枚举成员使用toString()方法 - how to use the toString() method for multiple enum members in same class in Java 如何在java中编译Object类的toString()方法? - How is the toString() method of Object class compiled in java? Java:调用方法,在一个类中初始化,在另一个类中给出 Nullpointerexception - Java:Calling method, initialized in one class, gives Nullpointerexception in other class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM