A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨杨 中级黑马   /  2013-3-2 15:01  /  1425 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杨杨 于 2013-3-2 15:03 编辑
  1. package cn.itcast.day1;

  2. public class ReflectPoint {
  3.         private int x;
  4.         private int y;
  5.         public String str1="ab";
  6.         public String str2="abc";
  7.         public ReflectPoint(int x,int y){
  8.                 /*System.out.println("x:"+x+"y:"+y);
  9.                 this.x=x;
  10.                 this.y=y;*/
  11.         }
  12.         public int getX() {
  13.                 return x;
  14.         }
  15.         public void setX(int x) {
  16.                 this.x = x;
  17.         }
  18.         public int getY() {
  19.                 return y;
  20.         }
  21.         public void setY(int y) {
  22.                 this.y = y;
  23.         }
  24.         public String getStr1() {
  25.                 return str1;
  26.         }
  27.         public void setStr1(String str1) {
  28.                 this.str1 = str1;
  29.         }
  30.         public String getStr2() {
  31.                 return str2;
  32.         }
  33.         public void setStr2(String str2) {
  34.                 this.str2 = str2;
  35.         }
  36.         
  37.         @Override
  38.         public int hashCode() {
  39.                 final int prime = 31;
  40.                 int result = 1;
  41.                 result = prime * result + ((str1 == null) ? 0 : str1.hashCode());
  42.                 result = prime * result + ((str2 == null) ? 0 : str2.hashCode());
  43.                 result = prime * result + x;
  44.                 result = prime * result + y;
  45.                 return result;
  46.         }
  47.         @Override
  48.         public boolean equals(Object obj) {
  49.                 if (this == obj)
  50.                         return true;
  51.                 if (obj == null)
  52.                         return false;
  53.                 if (getClass() != obj.getClass())
  54.                         return false;
  55.                 ReflectPoint other = (ReflectPoint) obj;
  56.                 if (str1 == null) {
  57.                         if (other.str1 != null)
  58.                                 return false;
  59.                 } else if (!str1.equals(other.str1))
  60.                         return false;
  61.                 if (str2 == null) {
  62.                         if (other.str2 != null)
  63.                                 return false;
  64.                 } else if (!str2.equals(other.str2))
  65.                         return false;
  66.                 if (x != other.x)
  67.                         return false;
  68.                 if (y != other.y)
  69.                         return false;
  70.                 return true;
  71.         }
  72.         @Override
  73.         public String toString(){
  74.                 return str1+str2;
  75.         }
  76. }
复制代码
  1. package cn.itcast.day1;

  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.HashSet;

  5. public class ReflectTest2 {

  6.         /**
  7.          * @param args
  8.          */
  9.         public static void main(String[] args) {
  10.                 Collection collection = new HashSet();
  11.                 ReflectPoint point1= new ReflectPoint(3,3);
  12.                 ReflectPoint point2= new ReflectPoint(3,4);
  13.                 ReflectPoint point3= new ReflectPoint(3,3);
  14.                 collection.add(point1);
  15.                 collection.add(point2);
  16.                 collection.add(point3);
  17.                 collection.add(point1);
  18.         //        point1.setY(7);
  19.                 collection.remove(point1);
  20.                 System.out.println(collection.size());
  21.                
  22.         }

  23. }
复制代码
构造方法RelefetPoint 类的构造函数 中的 this 加上注释后 输出size 变成0 求解释

评分

参与人数 1技术分 +1 收起 理由
张庚 + 1 赞一个!

查看全部评分

1 个回复

倒序浏览
你把它注释了,就会出现new ReflectPoint(x,y);创建对象时无论X、y等于多少,都会创建一个x=0、y=0(默认初始化值)的对象。
Collection collection = new HashSet();

                ReflectPoint point1= new ReflectPoint(3,3);

                ReflectPoint point2= new ReflectPoint(3,4);

                ReflectPoint point3= new ReflectPoint(3,3);

                collection.add(point1);

                collection.add(point2);

                collection.add(point3);

                collection.add(point1);


这样你的语句,其实就是只加了 一个对象 new ReflectPoint(0,0);


而后你又把它删除了
collection.remove(point1);

所以结果就为0咯

评分

参与人数 1技术分 +1 收起 理由
张庚 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马