Java 如何访问;名称“;在我的数组列表中,哪些是我接口类型的数组列表?
标题很冗长,可能会让人困惑,但我不知道如何让它更好…我希望能够访问数组列表中的值并将它们打印出来 我有一个叫做ThingBagInterface的接口。此ThingBagInterface只有一个方法,如下所示:Java 如何访问;名称“;在我的数组列表中,哪些是我接口类型的数组列表?,java,arraylist,interface,Java,Arraylist,Interface,标题很冗长,可能会让人困惑,但我不知道如何让它更好…我希望能够访问数组列表中的值并将它们打印出来 我有一个叫做ThingBagInterface的接口。此ThingBagInterface只有一个方法,如下所示: interface ThingBagInterface { public String getType(); } public void initCreatures(){ waterSnake = new Creature("Water Snake", Terrai
interface ThingBagInterface {
public String getType();
}
public void initCreatures(){
waterSnake = new Creature("Water Snake", Terrain.SWAMP, false, false, false, false, 1, 0);
etc...
}
public void populateBag(){
initCreatures();
bag.add(bears);
}
ArrayList<ThingBagInterface> bag = new ArrayList<ThingBagInterface>();
public Creature(String n, Terrain startTerrain, boolean flying, boolean magic, boolean charge, boolean ranged, int combat, int o){
name = n;
flyingCreature = flying;
magicCreature = magic;
canCharge = charge;
rangedCombat = ranged;
combatValue = combat;
owned = o;
}
我现在有一个叫做ThingBag的类,它是一个包,里面有很多不同的东西,比如生物,建筑等等
在我的ThingBag类中,我初始化了我所有的生物,如下所示:
interface ThingBagInterface {
public String getType();
}
public void initCreatures(){
waterSnake = new Creature("Water Snake", Terrain.SWAMP, false, false, false, false, 1, 0);
etc...
}
public void populateBag(){
initCreatures();
bag.add(bears);
}
ArrayList<ThingBagInterface> bag = new ArrayList<ThingBagInterface>();
public Creature(String n, Terrain startTerrain, boolean flying, boolean magic, boolean charge, boolean ranged, int combat, int o){
name = n;
flyingCreature = flying;
magicCreature = magic;
canCharge = charge;
rangedCombat = ranged;
combatValue = combat;
owned = o;
}
然后我有一个函数populateBag(),如下所示:
interface ThingBagInterface {
public String getType();
}
public void initCreatures(){
waterSnake = new Creature("Water Snake", Terrain.SWAMP, false, false, false, false, 1, 0);
etc...
}
public void populateBag(){
initCreatures();
bag.add(bears);
}
ArrayList<ThingBagInterface> bag = new ArrayList<ThingBagInterface>();
public Creature(String n, Terrain startTerrain, boolean flying, boolean magic, boolean charge, boolean ranged, int combat, int o){
name = n;
flyingCreature = flying;
magicCreature = magic;
canCharge = charge;
rangedCombat = ranged;
combatValue = combat;
owned = o;
}
我的数组列表定义在ThingBag中,如下所示:
interface ThingBagInterface {
public String getType();
}
public void initCreatures(){
waterSnake = new Creature("Water Snake", Terrain.SWAMP, false, false, false, false, 1, 0);
etc...
}
public void populateBag(){
initCreatures();
bag.add(bears);
}
ArrayList<ThingBagInterface> bag = new ArrayList<ThingBagInterface>();
public Creature(String n, Terrain startTerrain, boolean flying, boolean magic, boolean charge, boolean ranged, int combat, int o){
name = n;
flyingCreature = flying;
magicCreature = magic;
canCharge = charge;
rangedCombat = ranged;
combatValue = combat;
owned = o;
}
我想把熊的名字打印出来
所以我主要是这样做的:
ThingBag tb = new ThingBag();
tb.populateBag();
for(int i= 0; i<tb.bag.size(); i++){
System.out.println(i+". "+tb.bag.get(i));
}
但我现在不能。关于如何获取该值,有什么想法吗?我现在只能访问内存地址…您的
bag
变量声明为
ArrayList<ThingBagInterface> bag ...
阵列列表包。。。
这在概念上意味着它至少包含ThingBagInterface
对象。它可以包含任何类型的ThingBagInterface
,但至少必须是ThingBagInterface
这也意味着编译器只能保证它包含ThingBagInterface
,因此您只能作为ThingBagInterface
实例与其元素交互
名称
不是ThingBag界面
类型上存在的字段,它存在于生物
上
您可以强制转换
bag.get(i)
的返回值,或者声明ThingBagInterface
的getName()
方法,在子类型中实现它,并在循环中调用它。您的bag
变量声明为
ArrayList<ThingBagInterface> bag ...
阵列列表包。。。
这在概念上意味着它至少包含ThingBagInterface
对象。它可以包含任何类型的ThingBagInterface
,但至少必须是ThingBagInterface
这也意味着编译器只能保证它包含ThingBagInterface
,因此您只能作为ThingBagInterface
实例与其元素交互
名称
不是ThingBag界面
类型上存在的字段,它存在于生物
上
您可以强制转换
bag.get(i)
的返回值,或者声明ThingBagInterface
的getName()
方法,在子类型中实现它,并在循环中调用它。您需要更明智地设计ThingBagInterface
接口。有名字是一项要求吗?如果是这样,接口需要一种访问对象名称的方法。(这需要是一种方法,因为接口不能指定字段。)当我们这样做时,选择一个比ThingBagInterface
更具信息性的名称将是一个好主意。您需要更明智地设计ThingBagInterface
接口。有名字是一项要求吗?如果是这样,接口需要一种访问对象名称的方法。(这需要是一种方法,因为接口不能指定字段。)当我们这样做时,选择一个比ThingBagInterface
更具信息性的名称将是一个好主意。可能会像这样做System.out.println(i+”+((生物)tb.bag.)get(i));
这是因为接口没有name属性。请记住,接口中的字段始终隐式地为public static和final
我希望这能有所帮助。也许可以像这样做
System.out.println(I+“+((生物)tb.bag.)get(I));
这是因为接口没有name属性。请记住,接口中的字段始终隐式地为public static和final
我希望这能有所帮助。谢谢,这是我第一次尝试使用界面,所以在我真正弄明白之前,我会犯很多愚蠢的错误。再次感谢你。请注意,如果你要将元素投射到
生物
,没有太多理由使用ThingBagInterface
。ArrayList
莎拉会注意到USER 357112所说的。你有很多<代码> thigBug接口< /代码>吗?它们都有一个<代码>名称>代码吗?考虑为它们做一个基类。它们都有一个名字。我不确定是否真的想要一个接口或继承。我确实有一个数组的生物,但是我需要改变它。因为我的数组列表需要包含几种不同类型的东西。它们都有一个名称,所以也许这就是我应该去的地方,但名称是它们的所有共同点。谢谢,这是我第一次尝试使用接口,所以在我真正弄明白之前,我会犯很多愚蠢的错误。再次感谢你。请注意,如果你要NG将元素映射到<代码>生物< /代码>,根本没有理由有<代码> thigBug接口< /代码>。<代码> ARARYLIST//>将更好地工作。@莎拉注意到USER 357112刚才所说的。你有很多<代码> thigbAgbutoal吗?它们都有<代码>名称<代码>吗?考虑为它们做一个基类。所有人都有名字。我不确定我是否真的想要一个接口或继承。我确实有一个生物数组列表,但我需要更改它,因为我的数组列表需要包含几种不同类型的东西。它们都有一个名字,所以也许这就是我应该去的地方,但名称是它们所有的共同点。这是一个包的接口t持有“东西”这是我试图实现的棋盘游戏的游戏名称,所以它提供了大量信息,但我确实理解了你想要说的,所以谢谢you@Sarah:该名称没有提供任何有关它与ThingBag
的关系的信息。听起来像是ThingBag
应该实现它,而不是包含在一个袋子,伊玛吉