object类的学习笔记

概述

  • 超类、基类,所有类的直接或间接父类,位于继承树的最顶层。
  • 任何类,如没有书写extends显示继承某个类,都默认直接继承Object类,否则为间接继承。
  • Object类中所定义的方法,是所有对象都具备的方法。
  • Object类型可以存储任何对象。
  • 作为参数,可接受任何对象。
  • 作为返回值,可返回任何对象。

常用方法

getClass()方法

  • 返回引用中存储的实际对象类型。
  • 应用:通常用于判断两个引用中实际存储对象类型是否一致。
//案例演示
public class TestGetClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student S1 = new Student("aaa",20);
		Student S2 = new Student("bbb",20);
		Class class1=S1.getClass();
		Class class2=S2.getClass();
		//判断S1和S2是不是同一个类型
		if(class1==class2) {
			System.out.println("S1和S2属于同一个类型");
		}
		else {
			System.out.println("S1和S2不属于同一个类型");
		}


	}

}
class Student {
	private String name;
	private int age;
	public Student() {
		
	}
	public Student(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

hashCode()方法

  • 返回该对象的十进制的哈希码值。
  • 哈希算法根据对象的地址或字符串或数字计算出来的int类型的数值。
  • 哈希码并不唯一,可保证相同对象返回相同哈希码,尽量保证不同对象返回不同哈希码。
//案例演示
public class TestHashCode {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student S1 = new Student("aaa",20);
		Student S2 = new Student("bbb",20);
		//hashCode方法
		System.out.println(S1.hashCode());
		System.out.println(S2.hashCode());
		Student S3 = S1;
		System.out.println(S3.hashCode());
		
		


	}

}
class Student {
	private String name;
	private int age;
	public Student() {
		
	}
	public Student(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

  • 输出结果
  • 366712642
  • 1829164700
  • 366712642

toString()方法

  • 返回该对象的字符串表示(表现形式)。
  • 可以根据程序需求覆盖该方法,如:展示对象各个属性值。
//案例演示
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student S1 = new Student("aaa",20);
		Student S2 = new Student("bbb",20);
		//toString方法
		System.out.println(S1.toString());
		System.out.println(S2.toString());

		
		


	}

}
class Student {
	private String name;
	private int age;
	public Student() {
		
	}
	public Student(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	//按需求重写toString方法
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
}

重写=前的输出结果
csdn.Student@15db9742
csdn.Student@6d06d69c
重写后的输出结果
Student [name=aaa, age=20]
Student [name=bbb, age=20]

equals()方法

  • 默认实现为(this == obj),比较两个对象地址是否相同。
  • 可进行覆盖,比较两个对象的内容是否相同。
//案例演示
public class TestEquals {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student S1 = new Student("aaa",20);
		Student S2 = new Student("bbb",20);
		//toString方法
		System.out.println(S1.equals(S2));
		
		Student S3 = new Student("张三",20);
		Student S4 = new Student("张三",20);
		System.out.println(S3.equals(S4));

		
		


	}

}
class Student {
	private String name;
	private int age;
	public Student() {
		
	}
	public Student(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public boolean equals(Object obj) {
		//1判断两个对象是否是同一个引用
		if(this==obj) {
			return true;
		}
		//2判断obj是否null
		if(obj==null) {
			return false;
		}
		//3判断是否是同一个类型
//		if(this.getClass()==obj.getClass()) {
//			
//		}
		//intanceof 判断对象是否是某种类型
		if(obj instanceof Student) {
			//4强制类型转换
			Student s=(Student)obj;
			//5比较熟悉
			if(this.name.equals(s.getName())&&this.age==s.getAge()) {
				return true;
			}
			
			
		}
		return false;
	}
	
}

重写方法前的输出结果
false
false
重写方法后的输出结果
false
true

finalize()方法

  • 当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列。
  • 垃圾对象:没有有效引用指向此对象时,为垃圾对象。
  • 垃圾回收: 由GC销毁垃圾对象,释放数据存储空间。
  • 自动回收机制:JVM的内存耗尽,一次性回收所有垃圾对象。
  • 手动回收机制:使用System.gc(); 通知JVM执行垃圾回收。
//案例演示
public class TestFinalize {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		Student S1 = new Student("aaa",20);
//		Student S2 = new Student("bbb",20);
//		Student S3 = new Student("ccc",20);
//		Student S4 = new Student("ddd",20);
//		Student S5 = new Student("eee",20);
		new Student("aaa",20);
		new Student("bbb",20);
		new Student("ccc",20);
		new Student("ddd",20);
		new Student("eee",20);
		//回收垃圾
		System.gc();
		System.out.println("回收垃圾");

	}

}
class Student {
	private String name;
	private int age;
	public Student() {
		
	}
	public Student(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值