Home

韧桂

思考,总结

toString()方法的使用与练习

使用

package nine;

import java.util.Date;

/**
 * toString()方法:
 * java.lang.Object 类 toString()方法的定义如下:
 * public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
 1.当我们打印一个对象的引用时,实际上默认调用的就是这个对象的toString()方法
 2. 当我们打印的对象所在的类没有重写Object中的toString()方法时,那么调用的就是Object中定义的toString()方法,
 返回此对象所在的类及对应的堆空间对象实体的首地址值
 3.当我们打印的对象所在的类重写了toString()方法时,调用的就是我们自己重写的toString()方法。
 常常这样重写:将对象的属性信息返回。

 * @author  linjing
 * @see     java.lang.Class
 * @since   JDK1.0
 */
public class TestToString {
	public static void main(String[] args) {
		Person p1 = new Person("AA", 10);
		System.out.println(p1.toString());
		System.out.println(p1);//在没有重写 toString()时,输出地址。
		
		//4.像String类、包装类、File类、Date类等,已经实现了Object类中toString()方法的重写。
		String str = "AA";
		String str1 = new String("BB");
		System.out.println(str.toString());
		System.out.println(str1.toString());
		
		Date d = new Date();
		System.out.println(d);
	}
}	

练习

代码一

package nanedemo;

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;
    }

}

代码二

package nanedemo;

import java.util.Objects;

public class Circle extends  GeometricObject{
    private double radius;

    public Circle(){
//        this.color = "white";//不用重复写,默认调用父类的了
//        this.weight = 1.0;
        this.radius = 1.0;
    }
    public Circle(double radius){
        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(obj == this)
//            return true;
//        else if(obj instanceof Circle){
//            Circle c1 = (Circle)obj;
//            return this.radius == c1.radius;
//        }else {
//            return false;
//        }
//    }
//
//    public String toString(){
////        return radius + "";
//        return String.valueOf(radius);//将double类型转换为String
//    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Circle circle = (Circle) o;
        return Double.compare(circle.radius, radius) == 0;
    }



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

代码三

package nanedemo;

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));
        System.out.println(c1.toString());
    }
}

韧桂 2019-12-26