Problems encountered in Java programs

I encountered a problem today, and I can't figure it out for the time being, so write it down while it's hot

package com.practise.test;
import java.util.Map;
import
java.util.HashMap; public class ATestOfClassAndMap { static A TestFunction (A a) { A resA = new A(a); resA.av ++ ; resA.b.bv ++ ; return resA; } public static void main(String[] args) { A a = new A(); Map<String, B> testMap = new HashMap<String, B>(); for(int i = 0; i < 10; i++) { a.setValue(TestFunction(a)); testMap.put(String.format("%03d", i), a.b); System.out.println(String.format("%03d", i) + ":" + a.b.bv); } System.out.println("-----------------------------------------"); for(String strI:testMap.keySet()) { System.out.println(strI + ":" + testMap.get(strI).bv); } } } class A { int av ; B b; public A() { av = 0 ; b = new B(); } public A(A ta) { this .av = ta.av; this .b = new B (ta.b); } public void setValue(A ta) { this .av = ta.av; this .b.setValue (ta.b) ;; } } class B { int bv; public B() { bv = 100; } public B(B tb) { this.bv = tb.bv; } public void setValue(B tb) { this.bv = tb.bv; } }

//The output is as follows
--------------------------------------

000:101
001:102
002:103
003:104
004:105
005:106
006:107
007:108
008:109
009:110
-----------------------------------------
000:110
001:110
002:110
003:110
004:110
005:110
006:110
007:110
008:110
009:110

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324989236&siteId=291194637
Recommended