对象数组中不可访问的公共方法
我遇到的问题是无法在数组的对象元素中调用我自己的方法
这是带有数组的代码部分的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编译器无法知道这些对象是卡片,因为您在数组中将它们声明为
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
Object
s in the array. if you are sure that they will be cards, declare the array asCard
(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 realCard
instance before casting.System.out.println()
调用Object.toString()
,因此覆盖类 Card 的toString()
方法。 (顺便说一句:最好将Stringrank
和Stringsuit
都设为静态):System.out.println()
invokesObject.toString()
, So overwrite thetoString()
method of class Card. (btw: it's better to make theStringrank
andStringsuit
both static):