我正在嘗試使用鍵作為字符串和值作為靜態(tài)類創(chuàng)建一個(gè) Map。但是當(dāng)我打印數(shù)據(jù)時(shí),它只存儲(chǔ)最后一個(gè)鍵值對(duì)。有人可以幫我弄這個(gè)嗎。import java.util.HashMap;import java.util.Map;public class MapImplementation { public static class Asset { public static String assetName; public static String assetType; private void setAssetName(String name) { Asset.assetName = name; } private void setAssetType(String type) { Asset.assetType = type; } private String getAssetName() { return assetName; } private String getAssetType() { return assetType; } } public static void main(String[] args) { Map<String, Asset> map = new HashMap<>(); Asset asset1 = new Asset(); asset1.setAssetName("Vodafone"); asset1.setAssetType("STOCK"); map.put("Vodafone", asset1); Asset asset2 = new Asset(); asset2.setAssetName("Google"); asset2.setAssetType("STOCK"); map.put("Google", asset2); Asset asset3 = new Asset(); asset3.setAssetName("IBM"); asset3.setAssetType("BOND"); map.put("IBM", asset3); for (String str : map.keySet()) { Asset ast = map.get(str); System.out.println(ast.getAssetName()+" "+ast.getAssetType()); } }}我得到的輸出是:IBM BONDIBM BONDIBM BOND
靜態(tài)對(duì)象沒有存儲(chǔ)在 HashMap 中。!
炎炎設(shè)計(jì)
2021-09-29 15:02:34