当前位置: 代码迷 >> Java相关 >> 好心人帮个忙
  详细解决方案

好心人帮个忙

热度:263   发布时间:2010-05-09 12:21:21.0
好心人帮个忙
1)    设计一个Point类,该类包含两个int型成员变量:x、y,一个color型成员变量mycolor。请给出此类的构造方法,分别是一个不带参数的,一个带两个参数的,一个带三个参数的构造方法。还要给出对应的get方法和set方法,最后重写equals和toString方法。
搜索更多相关的解决方案: 设计  equals  color  

----------------解决方案--------------------------------------------------------
package cn.dadongzicool.point;

import java.awt.Color;

public class Point {
   
    int x,y;
    Color myColor;
   
    public Point() {
        super();
        // TODO Auto-generated constructor stub
    }
   
    public Point(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }
   
    public Point(int x, int y, Color myColor) {
        super();
        this.x = x;
        this.y = y;
        this.myColor = myColor;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Color getMyColor() {
        return myColor;
    }

    public void setMyColor(Color myColor) {
        this.myColor = myColor;
    }

    public boolean equals(Object obj) {
        return myColor.equals(obj);
    }

    public String toString() {
        return myColor.toString();
    }

}
----------------解决方案--------------------------------------------------------
重写equals方法必须要重写hashCode()方法
----------------解决方案--------------------------------------------------------
  相关解决方案