Java程序中遇到的问题

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

 1 package com.practise.test;
 2 import java.util.Map;
 3 import java.util.HashMap;
 4 
 5 public class ATestOfClassAndMap 
 6 {
 7     static A TestFunction (A a)
 8     {
 9         A resA = new A(a);
10         resA.av++;
11         resA.b.bv++;
12         return resA;
13     }
14     public static void main(String[] args)
15     {
16         A a = new A();
17         Map<String, B> testMap = new HashMap<String, B>();
18         
19         for(int i = 0; i < 10; i++)
20         {
21             a.setValue(TestFunction(a));
22             testMap.put(String.format("%03d", i), a.b);
23             System.out.println(String.format("%03d", i) + ":" + a.b.bv);
24         }
25         System.out.println("-----------------------------------------");
26         for(String strI:testMap.keySet())
27         {
28             System.out.println(strI + ":" + testMap.get(strI).bv);
29         }
30     }
31 
32 }
33 class A 
34 {
35     int av;
36     B b;
37     public A() 
38     {
39         av = 0;
40         b = new B();
41     }
42 
43     public A(A ta) 
44     {
45         this.av = ta.av;
46         this.b = new B(ta.b);
47     }
48     
49     public void setValue(A ta)
50     {
51         this.av = ta.av;
52         this.b.setValue(ta.b);;
53     }
54 }
55 
56 class B 
57 {
58     int bv;
59     public B() 
60     {
61         bv = 100;
62     }
63     
64     public B(B tb) 
65     {
66         this.bv = tb.bv;
67     }
68     
69     public void setValue(B tb)
70     {
71         this.bv = tb.bv;
72     }
73 }
74 
75 //输出结果如下
76 --------------------------------------
77 000:101
78 001:102
79 002:103
80 003:104
81 004:105
82 005:106
83 006:107
84 007:108
85 008:109
86 009:110
87 -----------------------------------------
88 000:110
89 001:110
90 002:110
91 003:110
92 004:110
93 005:110
94 006:110
95 007:110
96 008:110
97 009:110

 

已解决,原本的写法,每次插入的B的实例都是同一个,指向同一个地址,因此打印出来的bv值是一样的;将代码修改下就可以了

 1 package com.practise.test;
 2 import java.util.Map;
 3 import java.util.HashMap;
 4 
 5 public class ATestOfClassAndMap 
 6 {
 7     static A TestFunction (A a)
 8     {
 9         A resA = new A(a);
10         resA.av++;
11         resA.b.bv++;
12         return resA;
13     }
14     public static void main(String[] args)
15     {
16         A a = new A();
17         Map<String, B> testMap = new HashMap<String, B>();
18         
19         for(int i = 0; i < 10; i++)
20         {
21             a.setValue(TestFunction(a));
22             B tb = new B(a.b);
23 //            testMap.put(String.format("%03d", i), a.b);
24             testMap.put(String.format("%03d", i), tb);
25 //            System.out.println(String.format("%03d", i) + ":" + a.b.bv);
26 //            System.out.println(testMap);
27         }
28         System.out.println("-----------------------------------------");
29         for(String strI:testMap.keySet())
30         {
31             System.out.println(strI + ":" + testMap.get(strI).bv);
32         }
33     }
34 
35 }
36 class A 
37 {
38     int av;
39     B b;
40     public A() 
41     {
42         av = 0;
43         b = new B();
44     }
45 
46     public A(A ta) 
47     {
48         this.av = ta.av;
49         this.b = new B(ta.b);
50     }
51     
52     public void setValue(A ta)
53     {
54         this.av = ta.av;
55         this.b.setValue(ta.b);;
56     }
57 }
58 
59 class B 
60 {
61     int bv;
62     public B() 
63     {
64         bv = 100;
65     }
66     
67     public B(B tb) 
68     {
69         this.bv = tb.bv;
70     }
71     
72     public void setValue(B tb)
73     {
74         this.bv = tb.bv;
75     }
76     
77     public boolean equal(B tb)
78     {
79         return this.bv == tb.bv;
80     }
81 }

 

转载于:https://www.cnblogs.com/SawakoJ/p/8964781.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值