Bootstrap

第6章 Java常用类

Java程序设计基础-T6(Java常用类)

一、Object类

Object是所有Java类的父类,所有类都默认继承Object类。

image-20240422111034017

【实例1】

//java所有的类都默认继承Object类
public class Student {
   
	//测试
	public static void main(String[] args) {
   
		Student student = new Student();
		Student student1 = new Student();
		System.out.println(student.getClass());
		System.out.println(student.hashCode());
		System.out.println(student1.hashCode());
		System.out.println(student.equals(student1));
		//对象会默认调用toString()
		System.out.println(student);
	}
}

**【实例2】**重写toString()

//java所有的类都默认继承Object类
public class Student {
   
	private String name;
	private String sex;
	public String getName() {
   
		return name;
	}
	public void setName(String name) {
   
		this.name = name;
	}
	public String getSex() {
   
		return sex;
	}
	public void setSex(String sex) {
   
		this.sex = sex;
	}
	
	@Override
	public String toString() {
   
		return "Student [name=" + name + ", sex=" + sex + "]";
	}
	
	//测试
	public static void main(String[] args) {
   
		Student stu = new Student();
		stu.setName("张三");
		stu.setSex(&
;