简体   繁体   English

Java ArrayLists:将父类和子类添加到同一arraylist中,解决方案

[英]Java ArrayLists: Adding parent class and sub class to the same arraylist, solutions

I'm currently working on an independent project of writing a chess game. 我目前正在从事写象棋游戏的独立项目。 The issue I'm currently facing is I have a method that returns an arraylist of type Move (self explanatory) which is called when determining all of the legal moves. 我当前面临的问题是我有一个方法,该方法返回类型为Move(自我解释)的数组列表,在确定所有合法移动时将调用该列表。 While this works fine, I introduced a class called Castle, which extends Move (I did this because unlike a normal move, castling affects 4 squares and not just 2). 虽然效果很好,但我引入了一个名为Castle的类,该类扩展了Move(我这样做是因为与普通的移动不同,castling影响4个正方形,而不仅仅是2个正方形)。

I'd like the method getMoves() to be self contained, or be able to return all of the valid moves including objects of type Move and its child Castle. 我希望getMoves()方法是独立的,或者能够返回所有有效的移动,包括Move类型的对象及其子Castle。

The issue is if I include a declaration which adds valid castling moves to the method getMoves (which returns an array list of type move) I can either a.) add the arraylist as a type move, in which information is lost, or b.) a type castle, which changes the return arraylist to a type of all castle and obviously breaks the method. 问题是,如果我包含一个声明,该声明向有效方法getMoves(将返回类型为move的数组列表)添加有效的casting移动,我可以a。)将arraylist添加为丢失信息的类型move,或b。 )类型城堡,它将返回数组列表更改为所有城堡的类型,显然破坏了该方法。

Is there any graceful way to accomplish this? 有什么优美的方法可以做到这一点吗? I'm looking for ways to solve this problem without re-writing the move class while keeping the getMoves method stand alone. 我正在寻找解决此问题的方法,而无需重新编写move类,同时保持getMoves方法独立。

You can use arraylist of move objects and check for castle object using instanceof operator. 您可以使用移动对象的数组列表,并使用instanceof运算符检查城堡对象。 Then caste the object to castle type. 然后将对象投射为城堡类型。

if( moveobject instanceof Castle){
Castle castleobject = (Castle) moveobject;

// Do whatever u want to do with castleobject //做你想做的城堡对象

} }

Another option is that you can use generic here. 另一个选择是您可以在此处使用通用。 In your case, you want to return a list of either Move or Castle, or in global mean, a list of either Move or its sub-type. 对于您的情况,您想返回Move或Castle的列表,或者以总体平均值返回Move或其子类型的列表。 Below is example code: 下面是示例代码:

public <T extends Move> List<T> getMoves();

With this way, if you need to add another sub-type of Move other than Castle, you do not need to modify the return type any more. 这样,如果您需要添加“城堡”以外的其他“移动”子类型,则无需再修改返回类型。

It's hard to make a good design decision without seeing how everything is used, but here are a couple of solutions you could consider: 在不了解所有功能的情况下很难做出良好的设计决策,但是您可以考虑以下几种解决方案:

  1. Make a Move superclass of both BasicMove and Castling , then make your method return a list of that Move superclass. BasicMoveCastlingMove超类,然后使您的方法返回该Move超类的列表。 In this case your current Move class should be renamed to BasicMove . 在这种情况下,您当前的Move类应重命名为BasicMove

  2. Add a Castling as a field inside the Move class. 添加Castling的内场Move类。 You could save the rook's move inside the Move class, and the king's move inside the new Castling field. 您可以将车队的举动保存在Move类中,将国王的举动保存在新的Castling字段中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM