class Apple { private String color; private double weight; public Apple(){} public Apple(String color,double weight) { this.color = color; this.weight = weight; } public String toString() { //System.out.println("thecoloris:" + color,"theweightis:" + // weight); return "theapplecoloris:" + color + "theweightis:" + weight; } } public class ToStringTest { public static void main(String[] args) { Apple a = new Apple("red",23); System.out.println(a);//直接打印引用变量 } }
ToString方法的重写,可以实现自我描述,布布扣,bubuko.com
时间: 2024-10-29 19:06:51