日期:2014-05-20  浏览次数:20742 次

toString()方法怎么使用
//重写Point和Circle类的equals()、toString()方法,并测试这些方法的正确性。
class Point{
int x,y;
Point(){
x=0;y=0;
}
Point(int xx,int yy){
this.x=xx;
this.y=yy;
}
  int getX(){
  return x;
  }
  int getY(){
  return y;
  }
public boolean equals(Object obj){
Point p=(Point)obj;
if(x==p.x&&y==p.y)
return true;
else
return false;
}
public String toString(){
return ("点的横坐标:"+x+",点的纵坐标:"+y);
}
double distance(Point p)
{
  return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}
}
class sun{
public static void main(String args[]){
double d;
boolean a;
char b,c;
Point myPoint=new Point(1,1);
Point point1=new Point(1,2);
d=myPoint.distance(point1);
a=myPoint.equals(point1);
b=myPoint.toString();
c=point1.toString();
System.out.println("两点之间距离为:"+d);
System.out.println("两个点是否相等:"+a);
System.out.println("第一个点参数:"+b);
System.out.println("第二个点参数:"+c);
}
}
我不会调用toString方法,想请教高手这么调用对吗,要是该应该怎么改,谢谢!

------解决方案--------------------
用刚刚那个帖子的代码给你贴个例子
Java code


class Point{ //点类的定义
    private int x;
    private int y;

    public Point(){
       x=0;
       y=0;
    }

    @Override
    public String toString() {
        return "Point{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }

    public Point(int xx,int yy){
        this.x=xx; //this不加也可运行
        this.y=yy;
    }

    public int getX(){ //后面用到了
        return x;
    }
    public int getY(){
        return y;
    }
    public double distance(Point p){ //对象小写,类大写
        return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)); //用来求两点之间距离
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Point)) return false;

        Point point = (Point) o;

        if (x != point.x) return false;
        if (y != point.y) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = x;
        result = 31 * result + y;
        return result;
    }
}

class Circle{ //圆类的定义
    private int radius=0;
    //int x,y; //Point类型 圆心跟着Point走 不用再次定义x,y
    private Point center; //定义点类的圆心
    public Circle(){
        radius=1; //圆类的构造函数
        center=new Point(); //申请空间
    }

    @Override
    public String toString() {
        return "Circle{" +
                "radius=" + radius +
                ", center=" + center +
                '}';
    }

    public Circle(int xx,int yy,int r){
        radius=r;
        center=new Point(xx,yy); //因为没有定义Circle中的x,y;所以直接引用Point的构造函数即可
    }
    public Circle(Point c,int r){
        radius=r;
        center=c;
    }
    public double perimeter(){
        return 2*3.14*radius; //以刚开始声明的radius为准
    }
    public double area(){
        return 3.14*radius*radius;
    }
    public int relation(Circle c){
        int cx = c.center.getX(); //得到圆的圆心的横坐标
        int cy = c.center.getY();
        int cr = c.radius;
        int tx = this.center.getX();
        int ty = this.center.getY();
        Point myPoint=new Point(tx,ty); 
        Point point1=new Point(cx,cy);
        double distance1 = myPoint.distance(point1); //定义圆心距,圆心与圆心之间的距离,的通过对象调用distance;

        //distance1圆心距
        //第一个圆半径radius
        //第二个圆半径cr
        if(distance1 > (cr+radius)){
             return 0;//相离
        }else if(distance1 ==(cr+radius)){
             return 1;//外切
        }else if(distance1 < (cr+radius)&&distance1>Math.abs(cr-radius)){
             return 2;//相交
        }else if(distance1==Math.abs(cr-radius)){
             return 3;//内切
        }else if(distance1<Math.abs(cr-radius)){
             return 4;//包含
        }else{
            return -1;
        }

    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Circle)) return false;

        Circle circle = (Circle) o;

        if (radius != circle.radius) return false;
        if (!center.equals(circle.center)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = radius;
        result = 31 * result + center.hashCode();
        return result;
    }
}
public class Sun{
    

    public static void main(String[] args){
        double a,b,flag;
        Circle circle1=new Circle(12,12,4); //用来预备着求圆的位置关系的
        Circle circle2=new Circle(3,12,5);
        Circle circle3=new Circle(3,12,5);
        System.out.println("cricel1:"+circle1);
        System.out.println("circle1 equals(circle2) result:"+circle1.equals(circle2));
        System.out.println("circle3 equals(circle2) result:"+circle3.equals(circle2));

//        a=circle1.perimeter(); //上面改成无参的了
//        b=circle1.area();
//        flag=circle1.relation(circle2);
//        if(flag==0)
//            System.out.println("两个圆的关系是:两个圆为相离");
//        if(flag==1)
//            System.out.println("两个圆的关系是:两个圆为外切");
//        if(flag==2)
//            System.out.println("两个圆的关系是:两个圆为相交");
//        if(flag==3)
//            System.out.println("两个圆的关系是:两个圆为内切");
//        if(flag==4)
//            System.out.println("两个圆的关系是:两个圆为包含");
//        if(flag==-1)
//            System.out.println("两个圆的关系是:未知");
//        System.out.println("圆的周长是:"+a);
//        System.out.println("圆的面积是:"+b);
    }

}