gpt4 book ai didi

java - 如何避免instanceof?

转载 作者:行者123 更新时间:2023-11-29 05:50:13 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Anything wrong with instanceof checks here?

我有这个代码

public static int getNumberOfOwned(Player owner, String type){
int count = 0;
for (Field f: board.fieldList)
if (type == "Shipping" && f instanceof Shipping)
if (((Shipping)f).getOwnedBy() == owner)
count++;
else if (type == "Brewery" && f instanceof Brewery)
if (((Brewery)f).getOwnedBy() == owner)
count++;
return count;
}

我不认为这是非常优雅和面向 future 的。我怎样才能避免这些 instanceof 运算符。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com