如何在getter方法中返回两种类型之一?(How to return one of two types in a getter method?)

编程入门 行业动态 更新时间:2024-10-27 17:20:25
如何在getter方法中返回两种类型之一?(How to return one of two types in a getter method?)

我正在尝试为游戏编写一块棋盘。 该板有25个场,每个场可以包含一个底座或4个不同尺寸的环。

public class Field { private Base base; private Ring[] rings; public Object getPieces() { if (base == null) { return rings; } else { return base; } } }

我编写了setter方法,这样如果rings为null,则只能设置Base ,如果base为null,则只能设置Ring 。

问题是,为了绘制板,我的视图需要Field的内容,它可以是Base或Ring[] 。 但是,我觉得很难让方法返回两种类型。 现在,我将内容作为Object返回,但这是不好的做法。

有什么方法可以让我的方法返回两种类型?

如果没有,有没有一种方法可以让我更有效地存储我的作品,这使我可以将它们作为一种类型返回?

另外, Base和Ring都扩展了相同的超类Piece ,所以我可以将它们作为片段返回,但是我仍然会遇到这样的问题,即两个返回类型中的一个是另一个不是的数组。

I'm trying to program a board for a game. The board has 25 fields, and each field can either contain a base or 4 different sizes of rings.

public class Field { private Base base; private Ring[] rings; public Object getPieces() { if (base == null) { return rings; } else { return base; } } }

I programmed the setter methods so that a Base only can be set if rings is null, and that a Ring only can be set if base is null.

The problem is that in order to paint the board, my view needs the contents of Field, which is either a Base or a Ring[]. I find it hard, however, to allow the method to return both types. Right now, I'm returning the contents as Object, but this is bad practice.

Is there any way in which I can allow my method to return both types?

If not, is there a way in which I can store my pieces more efficiently that allows me to return them as one type?

Also, Base and Ring both extends the same superclass Piece, so I could return them as pieces instead, but then I would still have the problem that one of the two return types is an array where the other is not.

最满意答案

你可以返回一系列碎片。 如果你只需要返回基数,那么你只需将它放在一个将成为唯一元素的数组中。 否则返回数组或环。

所以,你的方法将是这样的:

public Piece[] getPieces() { Piece [] toReturn; if (base == null) { toReturn = rings; } else { toReturn = new Piece[]{base}; } return toReturn; }

考虑一下如果在Base类或Ring类中实现了新方法,那么这些方法就不能被调用,因为你定义了数组中的objets来自Piece类,除非你进行了转换 - 如果数组只有一个元素它是一个Base,否则就是一个Ring。 这就像一个implicit instanceOf ,所以要非常小心。 使用instanceOf应该让你检查是否有更好的方法来解决问题!

You could return an array of pieces. If you need to return only the base, then you just put it inside an array of pieces which will be the only element. Otherwise you return the array or rings.

So, your method will be like this:

public Piece[] getPieces() { Piece [] toReturn; if (base == null) { toReturn = rings; } else { toReturn = new Piece[]{base}; } return toReturn; }

Consider that if you implemented new methods in the Base or Ring classes, then those methods can´t be called because you defined that the objets inside the array are from the Piece class, unless you make a cast - if the array has only one element it is a Base, otherwise is a Ring. This is like an implicit instanceOf, so be very carefull. The use of instanceOf should make you check if there is a better way of solving the problem!

更多推荐

本文发布于:2023-08-04 15:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两种类型   方法   如何在   getter   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!