捕捉多个对象数组中的特定对象

huangapple go评论85阅读模式
英文:

Catch a specific object inside an Array of mutiple Objects

问题

我创建了一个名为“Object”的类,以及两个实现了“Object”类的类,“Item”和“Scenery”(每个物品都是一个对象,每个风景也是一个对象)。

在我的实现过程中,我创建了一个类型为Object的数组(让我们称之为ObjectArray)-(可以存储物品和风景)。

在代码的另一部分,我需要捕获ObjectArray中的特定元素,并且需要检查它是否是一个“Item”。

我找到了一个解决方案,但它太长了(每次添加物品到ObjectArray时,我都将它们添加到另一个仅包含物品的数组中,并检查我想要捕获的元素是否存在于这两个数组中)。

但我想可能有另一种更好的方法!

英文:

I created a class called "Object" and two classes "Item" and "Scenery" that implement the class Object ( every item is an object and every scenery is an object)

During my implementation I created an Array of type Object (let's call it ObjectArray)- (that can store both items and sceneries).

In another part of the code I need to catch a specific element from ObjectArray, and I need to check if it's an Item.

I found a solution but it's too long ( add every item I add ObjectArray to another array of only items and check if the element I want to catch exists in both arrays)

But I guess their might be another better way!?

答案1

得分: 2

> 我创建了一个名为"Object"的类,并创建了两个类"Item"和"Scenery",它们实现了类"Object"(每个物品都是一个对象,每个景观也是一个对象)

在Java中,每个对象都是Object类的实例,因此您不需要明确地使ItemScenery成为子类型。另外,Object是一个类,所以我们在抽象类型(如接口)上使用implement,在具体元素上使用extends

要确定对象的类型,您可以使用instanceof

Object[] myObj = {new Item(), new Item(), new Scenery()};
    
for (Object s : myObj) {
    if (s instanceof Item) {
        System.out.println(s.getClass() + " 它是一个物品!");
    }
}

请注意,instanceof也会对子类型返回true。因此,让我们假设在将来,您将使用另一个名为SubItem的类扩展Item类。如果对象数组包含一些SubItem实例,则在SubItem上的instanceof调用将返回true。因此,如果您想要精确确定它是否是正确的类对象,您需要进行更受限制的检查,就像s.getClass() == Item.class一样。

Object[] myObj = {new Item(), new Scenery(), new SubItem(), new SubItem()};
    
for (Object s : myObj) {
    if (s.getClass() == Item.class) {
        System.out.println(s.getClass() + " 它是一个物品!");
    }
}

在这种情况下,仅当s实际上是Item而不是SubItem时,此检查才会返回true。最后一点是不要使用Object,而是始终使用抽象类或接口来聚合更多类型。如果您继续学习,您将明白原因。因此,我建议您例如创建一个名为Shop的接口,然后让ItemScenery甚至SubItem都实现它。请注意,使用此方法,这3种类型都是Shop类型的子类型,因此将ItemScenerySubItemShop进行instanceof比较将返回true。

总结:在运行时检查时,instanceof对于抽象类型非常有用,您无法在静态上下文中确定对象的真实性质。(想象一下将实例化的Item赋值给它的Shop,例如Shop obj = new Item();。您会注意到这个赋值是合法的。)

英文:

> I created a class called "Object" and two classes "Item" and "Scenery"
> that implement the class Object ( every item is an object and every
> scenery is an object )

In Java, every object is an instance of Object, so you don't need explicitly to make Item and Scenery subtypes. Also, Object it's a class so we use to say implement with abstract types like Interfaces and extends for concrete elements.

To determinate what type is the object, you can use instanceof

Object[] myObj = {new Item(),new Item(), new Scenary()};

for (Object s : myObj)
     if (s instanceof Item)
        System.out.println(s.getClass() + " It's an item!");

Pay attention that instanceof return true also with subtypes. So let's assume in future you will extend the Item class with another class let's call it SubItem. If the array of objects contains some SubItem instances, the instanceof call on SubItem will return true. So if you want to determinate precisely if it's the right class object you need a more restricted check just like s.getClass() == Item.class.

Object[] myObj = {new Item(), new Scenary(), new SubItem(),new SubItem()};

for (Object s : myObj)
     if (s.getClass() == Item.class)
        System.out.println(s.getClass() + " It's an Item!"); 

In this latest scenario, this check will return true only if s is effectively a Item and not a SubItem.
As the latest point don't use Object, always use abstract class or interfaces to aggregate more types. If you will continue to study you will understand why. So what I'm saying is to create an interface for example Shop and let both Item Scenery and eventually SubItem implement it. Take notes that with this method both the 3 types are subtypes of the type Shop so a instanceof for each Item Scenery and SubItem compared with Shop will return true.
Summering: instanceof will be useful for runtime checks when you have an abstract type and you can't determinate statically the real nature of an object. (Imagine to assign an instantiated Item to it's Shop for example Shop obj = new Item();. You will notice that this assignment is legit.

huangapple
  • 本文由 发表于 2020年9月1日 01:26:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63675438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定