gpt4 book ai didi

Java 扫描对象

转载 作者:行者123 更新时间:2023-12-01 09:47:07 25 4
gpt4 key购买 nike

我有一个使用 Region 类中的对象的方法,因为该方法被另一个方法使用,我需要扫描该对象,以便可以应用该方法。

public void changeBusinessPermitTile(Region region){
if(getRegion()==null) {
Region i;
Scanner scanner = null;
try {
System.out.print("Please insert the region: ");
scanner = new Scanner(System.in);
i = scanner.//this is the missing part
setRegion(i);
} finally {
if (scanner!=null)
scanner.close();
}
}
}

所以我想要的是当我写作时

Region region1 = new Region(Landscape.COAST);
Region region2 = new Region(Landscape.HILL);
Region region3 = new Region(Landscape.MOUNTAIN);

当我写入region1、region2或region3时,该方法应该能够扫描

最佳答案

您可以阅读LandscapeString并初始化实例,例如

 Region r = new Region(Landscape.valueOf(scanner.nextLine()));

此外,您可以创建自己的小池( Map<String, Region> ),其中保存 Region 。实例。它看起来像 Flyweight pattern .

if (map.get(scanner.nextLine()) == null) {
// create an instance and put it in the map
} else {
// return old value from the map
}

关于Java 扫描对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37897370/

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