对象数组中不可访问的公共方法

发布于 2024-11-07 06:38:40 字数 1256 浏览 1 评论 0原文

我遇到的问题是无法在数组的对象元素中调用我自己的方法

这是带有数组的代码部分的代码:

public class CardRules {
    private Object cardArray[];

public CardRules(Object cardArrayCopy[]){

    cardArray = cardArrayCopy;
    multiples();
}
public void multiples(){
for(Object ArrayElement: cardArray){
    System.out.println(ArrayElement);
}
}
}

对于卡片对象:

public class Card {
    private int rank;
    private int suit;
    private String Stringrank[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    private String Stringsuit[] = {"Spades", "Hearts", "Diamonds", "Clubs"};
    public static int CardNo = 0;

public Card(int cardDetails[]){
    rank = cardDetails[1];
    suit = cardDetails[0];
    CardNo++;
}
public String getCard(){
    return ("Card: " + Stringrank[rank] + " Of " + Stringsuit[suit]);

}
   public int getRank(){
       return rank;
   }
   public int getSuit(){
       return suit;
   }

}

程序这部分的输出是哈希码, 卡@9304b1 卡@190d11 卡@a90653 卡@de6ced Card@c17164

我想提出类似或类似的内容,

System.out.println(ArrayElement.getRank());

有人知道为什么会发生这种情况吗?

顺便说一句,该数组是使用预制的 .toArray() 方法从另一个类中的 ArrayList 复制的

i have the problem of not being able to call my own methods in object elements of an array

Here is the code for the part of the code with the array:

public class CardRules {
    private Object cardArray[];

public CardRules(Object cardArrayCopy[]){

    cardArray = cardArrayCopy;
    multiples();
}
public void multiples(){
for(Object ArrayElement: cardArray){
    System.out.println(ArrayElement);
}
}
}

And for the Card Object:

public class Card {
    private int rank;
    private int suit;
    private String Stringrank[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    private String Stringsuit[] = {"Spades", "Hearts", "Diamonds", "Clubs"};
    public static int CardNo = 0;

public Card(int cardDetails[]){
    rank = cardDetails[1];
    suit = cardDetails[0];
    CardNo++;
}
public String getCard(){
    return ("Card: " + Stringrank[rank] + " Of " + Stringsuit[suit]);

}
   public int getRank(){
       return rank;
   }
   public int getSuit(){
       return suit;
   }

}

The output for this part of the program is the hash codes,
Card@9304b1
Card@190d11
Card@a90653
Card@de6ced
Card@c17164

i would like to put something like or similar

System.out.println(ArrayElement.getRank());

does anyone have any ideas as to why this is happening?

btw the array is copied from an ArrayList in another class using the premade .toArray() method

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本王不退位尔等都是臣 2024-11-14 06:38:40

编译器无法知道这些对象是卡片,因为您在数组中将它们声明为 Object。如果您确定它们是卡片,请将数组声明为 Card (private Card cardArray[];) 或将它们转换为 ((Card)ArrayElement)。 getRank()。

如果您想再检查一次,请在转换之前使用 ArrayElement instanceof Card 来验证这是一个真实的 Card 实例。

The compiler can't know those objects are cards, since you declare them as Objects in the array. if you are sure that they will be cards, declare the array as Card (private Card cardArray[];) or cast them ((Card)ArrayElement).getRank().

If you want to check one more time, use ArrayElement instanceof Card to validate that this is a real Card instance before casting.

花开柳相依 2024-11-14 06:38:40

System.out.println() 调用 Object.toString(),因此覆盖类 Card 的 toString() 方法。 (顺便说一句:最好将 StringrankStringsuit 都设为静态):

@Override
public String toString() {
    return String.format("Card [rank= %s, suit= %s]", 
           Stringrank[rank], Stringsuit[suit]);
}

System.out.println() invokes Object.toString(), So overwrite the toString() method of class Card. (btw: it's better to make the Stringrank and Stringsuit both static):

@Override
public String toString() {
    return String.format("Card [rank= %s, suit= %s]", 
           Stringrank[rank], Stringsuit[suit]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文