欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java程序中遇到的问题

程序员文章站 2022-07-08 09:49:37
今天遇到一个问题,暂时想不通,趁热先记下 000:101001:102002:103003:104004:105005:106006:107007:108008:109009:110 000:110001:110002:110003:110004:110005:110006:110007:11000 ......

今天遇到一个问题,暂时想不通,趁热先记下

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; } }

//输出结果如下
--------------------------------------

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