Object类

类 Object 是类层次结构的根类。每个类都使用 Object 作为超类。所有对象(包括数组)都实现这个类的方法。所有的类都直接或者间接的继承自Object类。该类的设计也符合面向对象中“万事万物皆对象”的思想。
构造方法 :
public Object()
Object类的特点
1.Object类是所有类的根类,可以利用多态创建对象
2.当一个方法的形参是Object类的时候,我们可以传入任意引用类型
3.Object满足万事万物皆对象的设计原则
4.创建任何一个类的对象都会去访问 Object 类的无参构造方法

举例代码如下

public class ObjectDemo01 {
	public static void main(String[] args) {
		Object object = new Student();
		
		test(new int[] {11,22,33});
		
		Student s = new Student();
	}
	
	public static void test(Object obj) {
		if (obj != null) {
			System.out.println("object");
		}
		
		if (obj instanceof Student) {
			
		}
		
	}
}

hashCode方法

实际上,由 Object 类定义的 hashCode 方法确实会针对不同的对象返回不同的整数。
(这一般是通过将该对象的内部地址转换成一个整数来实现的,但是 JavaTM 编程语言不需要这种实现技巧。)
1.hashCode方法会针对不同对象返回不同的整数
2.这个返回结果是内部地质转换而来的一个整数
3.hashCode相同能保证元素相同? – 不一定 地址相同能保证元素相同
==
比较基本数据类型 比的是数值本身
比较引用类型 比较的是真实地址
注意: 两个对象的hashCode相同,不能够保证两个对象是同一个对象

举例代码如下:

public class ObjectDemo02 {
	public static void main(String[] args) {
		Student s1 = new Student("隔壁老王", 18);
		Student s2 = new Student("隔壁老李", 28);
		Student s3 = new Student("隔壁老王", 18);
//		System.out.println(s1); 
//		System.out.println(s2);
//		System.out.println(s3);
//		
		System.out.println(s1 == s2);
		System.out.println(s1 == s3);
		
		System.out.println(s1.hashCode()); // 2018699554
		System.out.println(s2.hashCode()); // 1311053135
		System.out.println(s3.hashCode()); // 118352462
		
	}
}

getClass方法

字节码文件也是一个对象
类是用来描述事物的一个概念,概念本身使用Class类来描述
Class里面保存了一个类的所有信息: 成员变量,成员方法,构造方法…
s1.getClass().getName()返回的是类名的全路径

举例代码如下:

public class ObjectDemo03 {
	public static void main(String[] args) {
		Student s1 = new Student("隔壁老王", 18);
		Student s2 = new Student("隔壁老李", 28);
		
		Class c1 = s1.getClass(); // Student.class
		Class c2 = s2.getClass(); // Student.class
		
		System.out.println(c1 == c2);
		
		String name = c1.getName();
		System.out.println(name); // com.sxt.objectdemo01.Student
		String name2 = c2.getName();
		System.out.println(name);
		
		String name3 = s1.getClass().getName();
		System.out.println(name3);
	}
}

toString方法

观察源码:

public String toString() {
	        return s1.getClass().getName() + "@" + Integer.toHexString(2018699554);
	        com.sxt.objectdemo01.Student@7852e922
	    }
	    public void println(Object x) {
		// Object x = s1;
        String s = String.valueOf(x);
        synchronized (this) {
            print(s);
            newLine();
        }
    }
    Object obj =  new Student("隔壁老王", 18);
    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }

默认输出一个对象,输出的是该对象 toString方法
默认输出的是类名全路径+@+对象的hashCode的十六进制转换,毫无意义
如果一个类的成员有100个,那么我一个一个重写很慢
为什么输出一个对象输出的是toString方法

举例代码如下:

public class ObjectDemo04 {
	public static void main(String[] args) {
		Student s1 = new Student("隔壁老王", 18);
		Student s2 = new Student("隔壁老李", 28);
		
//		System.out.println(s1.hashCode());
//		String ss = s1.toString();
//		System.out.println(ss); // com.sxt.objectdemo01.Student@7852e922
		
		Student s3 = null;
		System.out.println(s3);
		System.out.println(s1); // com.sxt.objectdemo01.Student@7852e922
		System.out.println(s2);
	}
}

equals方法

通过源码我们发现 Object方法中的equals比较的是地址值
比较两个对象是否相等,比较地址值毫无意义

我们希望比较两个对象,比较的是成员变量是否相等,所以说父类Object继承过来的equals方法
不能满足具体子类Student的需求,所以需要方法重写

我们一般开发中使用自动生成
alt+shift+s 再按H

通过观察自动生成的代码我们发现字符串比较的是内容是否相等
举例代码如下:


public class ObjectDemo05 {
	public static void main(String[] args) {
		Student s1 = new Student("隔壁老王", 18);
		Student s2 = new Student("隔壁老李", 28);
		Student s3 = new Student(null, 18);
		
		System.out.println(s1.equals(s2)); // false 
		System.out.println(s1.equals(s3)); // true
		System.out.println(s1.equals(null)); // true
		System.out.println(s1.equals(s1)); // true
		
//		System.out.println("abc".equals("abd"));
	}
}

clone方法

创建并返回此对象的一个副本。
java.lang.CloneNotSupportedException
错误名称: 克隆不支持异常
产生原因: 需要克隆的类没有实现克隆接口
解决办法: 找到对应的类实现Cloneable接口

举例代码如下:

public class ObjectDemo06 {
	public static void main(String[] args) throws CloneNotSupportedException {
		Student s1 = new Student("张飞", 20);
		Student s2 = new Student("曹操", 30);
		
		Object s1Clone = s1.clone();
		
		Student s3 = (Student)s1Clone;
		System.out.println(s3.getName() + "|" + s3.getAge());
		
		Student s4 = s1;
		System.out.println(s1 == s4); // true
		System.out.println(s1 == s3); // false
		
		s3.setName("刘备");
		s3.setAge(30);
		
		System.out.println(s1.getName() + "|" + s1.getAge());
		System.out.println(s3.getName() + "|" + s3.getAge());
	}
}
public class Student extends Object implements Cloneable{
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}
public interface Cloneable {
}

finalize()方法

当垃圾回收器确定不存在对该对象的更多引用时,
由对象的垃圾回收器调用此方法。用于垃圾回收,但是什么时候回收不确定。

面试题
什么情况下对象会被当做垃圾处理?
1.当对象被赋null
Student s = new Student();
s = null;
2.当对象重新被创建
Student s = new Student();
s = new Student();
3.对象所在的方法被调用完后
4.匿名对象第一次使用完毕后
面试题
finalize()方法,finally关键字,final关键字的区别?
finalize()方法
当垃圾回收器确定不存在对该对象的更多引用时,
由对象的垃圾回收器调用此方法。用于垃圾回收,但是什么时候回收不确定。
final
final修饰的变量表示常量,不能够被二次赋值
final修饰的类不能够被继承
final修饰的方法不能够被子类重写
finally
后续会整理,一般用来释放资源(io流的端口,网络的端口,数据库的连接断开等等)

举例代码如下:

public class ObjectDemo07 {
	public static void main(String[] args) throws Throwable {
		Student s1 = new Student();
		s1.finalize();
		s1 = null;
		
	}
}

public class Student{
	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
	}
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值