首页 > 技术文章 > toString()方法的使用

afangfang 2020-03-18 20:21 原文

toString()方法:

java.lang.Object类的toString()方法的定义如下:

public String toString(){

              return getClass().getName()+"@"+Integer.toHexString(hashCode());

      }

1.当打印一个对象的引用时,实际上默认调用的就是这个对象的toString()方法

2.当打印的对象所在的类没有重写Object中的toString()方法时, 那么调用的就是Object中定义toString()方法。

返回此对象所在的类及对应的堆空间实体的首地址值。

3.当打印的对象所在的类重写了toString()方法时,调用的就是我们自己重写的toString()方法;

注:常这样重写:将对象的属性信息返回,

4.像String类   包装类,File类  Date类等,已经实现了Object类中toString()方法的重写。

 

TestToString:

package com.aff.equals;

import java.util.Date;

import com.aff.java1.Person;

public class TestToString {
    public static void main(String[] args) {
        Person p1 = new Person("AA", 10);
        System.out.println(p1.toString());// com.aff.java1.Person@6d06d69c
        System.out.println(p1);// com.aff.java1.Person@6d06d69c

        String str = "AA";
        String str1 = new String("BB");
        System.out.println(str);
        System.out.println(str1.toString());

        Date d = new Date();
        System.out.println(d);
    }

}

输出结果:

com.aff.java1.Person@6d06d69c
com.aff.java1.Person@6d06d69c
AA
BB
Wed Mar 18 19:14:58 CST 2020

 

 

 

 

注意:double类型转为String类型 :String.valueOf(radius)

e2:

Circle:

package com.aff.equals;

public class Circle extends GeometricObject {
    private double radius;

    public Circle() {
        super();
        this.radius = 1.0;
    }

    public Circle(double radius) {
        super();
        this.radius = radius;
    }

    public Circle(double radius, String color, double weight) {
        super(color, weight);
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    // 圆的面积
    public double findArea() {
        return Math.PI * radius * radius;
    }

    // 重写equals()方法和toString()

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj instanceof Circle) {
            Circle c = (Circle) obj;
            return this.radius == c.radius;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        return "Circle [radius=" + radius + "]";
    }

/*    public String toString() {
        return radius + "";
    }*/

    
}

 

 

GeometricObject:

package com.aff.equals;

public class GeometricObject {
    protected String color;
    protected double weight;

    public GeometricObject() {
        super();
        this.color = "white";
        this.weight = 1.0;
    }

    
    public GeometricObject(String color, double weight) {
        super();
        this.color = color;
        this.weight = weight;
    }



    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

}

 

TestCircle:

package com.aff.equals;

public class TestCircle {
    public static void main(String[] args) {
        Circle c1 = new Circle(2.3);
        Circle c2 = new Circle(2.3);
        System.out.println(c1.equals(c2));// false-->true
        System.out.println(c1.toString());
    }
}

输出结果:

true
Circle [radius=2.3]

 

 

推荐阅读