作者:少女24梦_276 | 来源:互联网 | 2022-12-02 18:04
容器可以包含属于人的自行车和椅子.我想检查,如果容器包含自行车或所述人的椅子.没有使用这可能instanceof
吗?
public class Container {
public Map> items = new HashMap<>();
public void add(Person p, Item item) {
items.get(p).add(item);
}
public boolean containsChair(Person owner) {
for(Item i : items.get(owner)) {
if(i instanceof Chair) {
return true;
}
}
return false;
}
public boolean containsBike(Person owner) {
for(Item i : items.get(owner)) {
if(i instanceof Bike) {
return true;
}
}
return false;
}
}
出于说明的目的,Item,Bike,Chair,Person都是最简单的类存根:
public class Person { public String name; }
public abstract class Item {}
public class Bike extends Item { public Wheel[] wheels;}
public class Chair extends Item { public Leg[] legs;}
public class Wheel {}
public class Leg {}
在跑步者中,一个人应该能够在其容器中添加椅子和自行车:
import java.util.ArrayList;
public class Runner {
public static void main(String[] args) {
Container c = new Container();
Person p = new Person();
// Prevent null pointer exception
c.items.put(p, new ArrayList<>());
c.add(p, new Chair());
// True
System.out.println(c.containsChair(p));
}
}
LuCio..
5
您可以向类中添加Item
一个抽象方法ItemType getType()
.ItemType
将是枚举所有可能的项类型的枚举.
public abstract class Item {
public abstract ItemType getType();
}
public enum ItemType {
BIKE, CHAIR;
}
执行Chair
:
public static class Chair extends Item {
public Leg[] legs;
@Override
public ItemType getType() {
return ItemType.CHAIR;
}
}
然后你可以定义一个contains
搜索给定的方法,Person
如果它有一个具有某个特定的项目ItemType
:
public boolean contains(Person owner, ItemType itemType) {
return items.get(owner).stream().anyMatch(item ->itemType.equals(item.getType()));
}
或者关于owner
s items
列表的空安全:
public boolean contains(Person owner, ItemType itemType) {
return Optional.ofNullable(items.get(owner))
.map(i -> i.stream().anyMatch(item -> itemType.equals(item.getType())))
.orElse(false);
}
用法:
public static void main(String[] args) {
Container c = new Container();
Person p = new Person();
// Prevent null pointer exception
c.items.put(p, new ArrayList<>());
c.add(p, new Chair());
// True
System.out.println(c.contains(p, ItemType.CHAIR));
}
编辑
按照这种方法,不需要instanceof
检查.使用instanceof
可以是一个提示,表明该设计有一些缺陷.
1> LuCio..:
您可以向类中添加Item
一个抽象方法ItemType getType()
.ItemType
将是枚举所有可能的项类型的枚举.
public abstract class Item {
public abstract ItemType getType();
}
public enum ItemType {
BIKE, CHAIR;
}
执行Chair
:
public static class Chair extends Item {
public Leg[] legs;
@Override
public ItemType getType() {
return ItemType.CHAIR;
}
}
然后你可以定义一个contains
搜索给定的方法,Person
如果它有一个具有某个特定的项目ItemType
:
public boolean contains(Person owner, ItemType itemType) {
return items.get(owner).stream().anyMatch(item ->itemType.equals(item.getType()));
}
或者关于owner
s items
列表的空安全:
public boolean contains(Person owner, ItemType itemType) {
return Optional.ofNullable(items.get(owner))
.map(i -> i.stream().anyMatch(item -> itemType.equals(item.getType())))
.orElse(false);
}
用法:
public static void main(String[] args) {
Container c = new Container();
Person p = new Person();
// Prevent null pointer exception
c.items.put(p, new ArrayList<>());
c.add(p, new Chair());
// True
System.out.println(c.contains(p, ItemType.CHAIR));
}
编辑
按照这种方法,不需要instanceof
检查.使用instanceof
可以是一个提示,表明该设计有一些缺陷.