Object类学习


  

1 Obeject类

  1、Object类是Java所有类的根父类
  2、Java中继承是可传递的, Object类的方法,所有类都能继承到

2 Obeject类的方法

返回值类型说明
protectedObject clone() 对象克隆, 在堆中再创建一个完全一样的对象
booleanequals(Object obj) 判断两个对象的内容是否一样.
protected voidfinalize() 垃圾回收器在回收某个对象时,会调用对象的finalize()方法.垃圾回收器在什么回收这个对象不确定,这个方法的执行时间不确定, 一般不用
Class<?> getClass()返回对象的运行时类对象. 可以简单的理解为返回对象类的字节码文件
inthashCode() 返回对象的哈希码
voidnotify() 唤醒线程
StringtoString() 把对象转换为字符串
voidwait() 线程等待

3 对象的深复制和浅复制

在这里插入图片描述

4 实体类一般提供哪些方法

  实体类一般情况下 :
  1) 把所有的字段都设置为私有的
  2) 只提供无参构造方法,不提供有参构造方法
  3) 重写equals()/hashCode()
  4) 重写toString()
  5) 提供公共的getter/setter方法


public class Book {
	private String name;
	private String author;
	private String press;
	private String isbn;
	private double price;
	
	public Book() {
		super();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((author == null) ? 0 : author.hashCode());
		result = prime * result + ((isbn == null) ? 0 : isbn.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((press == null) ? 0 : press.hashCode());
		long temp;
		temp = Double.doubleToLongBits(price);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (author == null) {
			if (other.author != null)
				return false;
		} else if (!author.equals(other.author))
			return false;
		if (isbn == null) {
			if (other.isbn != null)
				return false;
		} else if (!isbn.equals(other.isbn))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (press == null) {
			if (other.press != null)
				return false;
		} else if (!press.equals(other.press))
			return false;
		if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Book [name=" + name + ", author=" + author + ", press=" + press + ", isbn=" + isbn + ", price=" + price
				+ "]";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPress() {
		return press;
	}

	public void setPress(String press) {
		this.press = press;
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}
	
}

5 重写toString和equals方法

public class Person {
	public String name;
	int age;
	String gender;
	
	public Person( String name, int age, String gender){
		this.name = name;
		this.age = age;
		this.gender = gender;
	}
	
	//重写toString()
	@Override
	public String toString() {
		return  "姓名," + name + ",年龄:" + age + ",性别:" + gender; 	
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	//重写equals()方法
	@Override
	public boolean equals(Object obj) {
		//1)如果两个变量引用了同一个对象,就返回true
		if (this == obj)
			return true;
		//2)如果参数对象为null, 返回false
		if (obj == null)
			return false;
		//3)如果两个对象的类型不一样,就返回false
		if (getClass() != obj.getClass())
			return false;
		//4) 相同类型的两个对象, 如果有一个字段不相同就返回false
		Person other = (Person) obj;
		if (age != other.age)
			return false;
		if (gender == null) {
			if (other.gender != null)
				return false;
		} else if (!gender.equals(other.gender))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	
	
}

6 测试重写toString

  作用:
  把对象转换为字符串
  应用场景:
  当使用System.out.println( obj ) 打印obj对象时, 会调用对象的toString()方法
  怎么用toString()?
  如果使用System.out.println( obj ) 打印obj对象时, 想要显示对象的各个字段值时, 就重写toString()
  打印一个对象时,如果显示的不是完整类名@十六进制数形式, 说明该类或者该类的父类重写了toString()方法

/**
 * 测试toString()方法
 *
 */
public class Test01 {

	public static void main(String[] args) {
		//创建Person对象
		Person p1 = new Person("lisi", 18, "女");
		//直接把p1打印到屏幕上
		System.out.println( p1 );
		
		/*
		 * 打印结果:
		 * 		com.object.Person@15db9742
		 * 			完整类名 @ 十六进制数
		 * 
		 * 分析为什么会 打印出这个结果?
		 * 		在使用System.out.println( obj ) 打印一个对象时, 会调用对象的toString()方法
		 * 		toString()是从Object类继承来的, 在Object类中toString()方法体返回以下内容:		
		 * 			return getClass().getName() + "@" + Integer.toHexString(hashCode());
		 * 						完整类名          	    + "@" +   把哈希码转换为十六进制
		 * 
		 * 需求:
		 * 		在打印p1对象时, 想显示p1对象的各个字段值, 姓名:lisi,年龄:18,性别:女
		 * 
		 * 解决思路:
		 * 		在使用System.out.println( p1 ) 打印p1对象时, 会调用p1对象的toString()方法
		 * 		toString()是从Object类继承来的, 默认返回完整类名@十六进制数
		 * 		即从Object类继承的toString()方法不能满足子类需求
		 * 		需要重写toString()方法
		 */
		
		String s1 = "aa";
		System.out.println( s1 );  	//String类重写了toString()	
		Integer xx = new Integer(456);
		System.out.println( xx ); 	//Integer类重写了toString()
		ArrayList list = new ArrayList<String>();
		System.out.println( list ); //ArrayList类重写了toString()
	}

}

姓名,lisi,年龄:18,性别:女
aa
456
[]

Process finished with exit code 0

7 测试equals方法

  作用:
  比较两个对象的内容,即两个对象的各个字段值是否相同
  如何用?
  如果想要比较这个类两个对象内容是否一样,需要重写equals()方法
  注意:
  根据哈希约定, 如果两个对象equals()相等,这两个对象的hashcode也应该相等,即在重写equals()方法的同时,也需要重写hashCode()方法

/**
 * 测试equals()方法
 *
 */
public class Test02 {

	public static void main(String[] args) {
		Person p1 = new Person("lisi", 18, "女");
		Person p2 = new Person("lisi", 18, "女");
		
		System.out.println( p1 == p2 ); 		// false
		/*
		 * 关系运算符== 判断两个变量的值是否相等
		 *  p1 = new Person("lisi", 18, "女"); new运算符在堆中创建一个对象, 把该对象的引用赋值给p1
		 *  p2 = new Person("lisi", 18, "女"); new运算符在堆中创建一个对象, 把该对象的引用赋值给p2
		 *  现在p1和p2分别保存两个对象的引用
		 */
		
		//判断两个对象的内容是否相同, 需要调用对象的equals()方法
		System.out.println( p1.equals(p2) ); 		//
		/*
		 * 期望p1.equals(p2)返回true, 现在返回的是false
		 * equals()方法是从Object类继承来的, 在Object类中equals()方法也是使用 == 进行判断的
		 * 如果想让p1.equals(p2) 判断两个对象的字段是否都 一样, 需要在Person类中重写equals()方法		  
		 */
		
		//1)如果两个变量引用了同一个对象,就返回true
		Person p3 = p1;
		System.out.println( p1.equals(p3));
		//2)如果参数对象为null, 返回false
		p3 = null;
		System.out.println( p1.equals(p3) );  //p1如果为null产生空指针异常
		//3)如果两个对象的类型不一样,就返回false
		String text = "hehe";
		System.out.println( p1.equals(text) );
		//
		Person p4 = new Person("lisi", 18, "男");
		System.out.println( p1.equals(p4) );
	}

}
false
true
true
false
false
false

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值