6.2.1 打印对象和toString方法
2015-04-06 18:40
387 查看
package chap6_2; class Person { private String name; public Person() { // TODO Auto-generated constructor stub this.name=name; } } public class PrintObject { public static void main(String[] args) { // TODO Auto-generated method stub Person p=new Person(); System.out.println(p); } }
运行结果
chap6_2.Person@1db9742
Object类提供的toString()方法总是返回该对象实现类的“类名+@+hashCode”值
package chap6_2; class Apple { private String color; private double weight; public Apple() { // TODO Auto-generated constructor stub } public Apple(String color,double weight) { 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; } @Override public String toString() { return "一个苹果,颜色是" + color + ",重量是" + weight ; } } public class ToStringTest { public static void main(String[] args) { Apple a=new Apple("红色",5.68); System.out.println(a); // TODO Auto-generated method stub } }
相关文章推荐
- 打印对象和toString方法
- java 打印对象时时如何调用对象toString()方法的
- 当使用System,out.println()打印一个对象是自动调用toString方法
- 处理对象(1)-打印对象和toString方法
- 原!! java直接打印一个对象时,并不是直接调用该类的toString方法 ,而是会先判断是否为null,非null才会调用toString方法
- 打印对象和ToString方法
- 打印对象和toString方法
- 打印对象和toString方法
- 重写toString方法 打印对象为json
- 一个对象toString()方法如果没有被重写,那么默认调用它的父类Object的toString()方法,而Object的toString()方法是打印该对象的hashCode,一般hashCode就是此对象的内存地址
- Java 打印对象和toString 方法
- java打印对象和toString方法
- 打印对象和toString方法
- java在调用System.out.println()打印一个新new的对象时,为什么会调用toString方法?
- Java的打印对象和toString方法
- 关于打印未定义toString方法的对象
- System.out.println(s) 打印值为对象引用时,调用toString()方法。
- System.out.println(s) 打印值为对象引用时,调用toString()方法。
- java在调用System.out.println()打印一个新new的对象时,为什么会调用toString方法?
- JQuery each打印JS对象的方法