当前位置: 移动技术网 > IT编程>开发语言>Java > Java程序中遇到的问题

Java程序中遇到的问题

2018年04月28日  | 移动技术网IT编程  | 我要评论

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

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

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网