對象相等的條件

public static class Character {

        public final String name;
        public final String age;

        public Character(String name, String age) {
            this.name = name;
            this.age = age;
        }
    }
    public static  void putAndGet(){
        Map<Character,Integer> map = new HashMap<>();
        Character xd = new Character("aaa","111");
        map.put(xd,1);
        map.put(new Character("bbb","222"),2);
        System.out.println(map.get(xd));
        System.out.println(map.get(new Character("aaa","111")));
    }

    public static void main(String[] args) {
        putAndGet();
        // 求輸出結果,本題沒有給出外面的類:
             //若是外面類不存在,直接報錯
             //若是不考慮這個,輸出結果爲:
                  //1
                  //null
    }
相關文章
相關標籤/搜索