0
点赞
收藏
分享

微信扫一扫

如何重写toString方法 一般为调试程序需要

辰鑫chenxin 2022-02-27 阅读 44
java

 

package com.API;

/*
 * toString()方法返回的该对象的字符串表示
 * 	return get.Class().getName()+"@"+Integer.toHexString(hashCode());
 * 		   get.Class()返回一个字节码对象
 * 		   Integer.toHexString()返回指定参数的十六进制字符串形式
 * 		   hashCode():返该对象的哈希码值(内部地址转化为的整数)
 */
public class ObjectDemo {
	public static void main(String[] args) {
		Student s = new Student();
		s.age = 19;
		s.name = "六六";
		//说明我们输出一个对象默认是输出这个对象的toString方法
		System.out.println(s);//com.API.Student@26f0a63f
		System.out.println(s.toString());//com.API.Student@26f0a63f
		System.out.println(s.hashCode());//653305407
		System.out.println(Integer.toHexString(s.hashCode()));//26f0a63f
	}
}

//不写继承也可以 ,Object类层次结构的根类。每个类都使用Object作为超类。
//class Student {
class Student extends Object {
	String name;
	int age;
}
举报

相关推荐

0 条评论