Java基础之Object类

Java基础

API

1.常用的工具包
java.long:String,System,Thread,Math,Object等,自动导入
java.io:输入输出流
java.net:网络编程
java.util:工具类,集合,时间日期类等

Object

1.Object类
clone();
自定义的类,使用clone方法,当前类必须要实现Cloneable接口;
因为object类中的clone是protected修饰的,所以使用时可能要重写该方法(把该方法的访问修饰符扩大)。
浅克隆:对于引用变量,克隆的是地址。
深克隆:对于引用变量,克隆了一个新的对象。

	package com.java.day9_2;
	
	public class Profesor {
		private int age;
	
		public int getAge() {
			return age;
		}
	
		public void setAge(int age) {
			this.age = age;
		}
		
	}

	package com.java.day9_2;
	
	public class Student implements Cloneable{
		private int age;
		private Profesor p;
		public Profesor getP() {
			return p;
		}
	
		public void setP(Profesor p) {
			this.p = p;
		}
	
		public int getAge() {
			return age;
		}
	
		public void setAge(int age) {
			this.age = age;
		}
		
		@Override
		public Object clone() throws CloneNotSupportedException {
			return super.clone();
		}
	}

	package com.java.day9_2;
	//克隆用法举例以及浅克隆说明
	public class MainClass {
		public static void test() throws CloneNotSupportedException {
			Student stu = new Student();
			stu.setAge(18);
			Profesor p = new Profesor();
			stu.setP(p);
			stu.getP().setAge(28);
			Student stu2 = (Student)stu.clone();
			stu2.setAge(20);
			stu2.getP().setAge(38);
			System.out.println(stu.getAge());
			System.out.println(stu2.getAge());
			System.out.println(stu.getP().getAge());
		}
		public static void main(String[] args) throws CloneNotSupportedException {
			test();
		}
	
	}
	package com.java.day9_2;
	//深克隆说明,接上面部分代码
	public class Profesor implements Cloneable{
		private int age;
	
		public int getAge() {
			return age;
		}
	
		public void setAge(int age) {
			this.age = age;
		}
		
		@Override
		public Object clone() throws CloneNotSupportedException {
			return super.clone();
		}
	}
	
	package com.java.day9_2;

	public class Student implements Cloneable{
		private int age;
		private Profesor p;
		public Profesor getP() {
			return p;
		}
	
		public void setP(Profesor p) {
			this.p = p;
		}
	
		public int getAge() {
			return age;
		}
	
		public void setAge(int age) {
			this.age = age;
		}
		
		@Override
		public Object clone() throws CloneNotSupportedException {
			Student stu = (Student)super.clone();
			stu.p = (Profesor)this.p.clone();
			return stu;
		}
	}

toString
返回对象的字符串表示形式
包名.类名@hashCode(内存地址)
当打印对象时,默认调用
equals
equals方法比较两个对象是否相同 == 比较的是地址

	package com.java.day9_3;
	
	public class EMP {
		private String str;
	
		public EMP(String str) {
			super();
			this.str = str;
		}
		
	}
	package com.java.day9_3;
	
	public class Test {
	
		public static void main(String[] args) {
			EMP emp1 = new EMP("hello");
			EMP emp2 = new EMP("hello");
			System.out.println(emp1.equals(emp2));
			String str1 = new String("hello");
			String str2 = new String("hello");
			System.out.println(str1.equals(str2));
		}
	
	}

hashCode
返回一个hash code码,Integer,内存地址有关的hash算法。
把内存地址及其它返回一个整型值。
如果重写了equals(如上面的emp1.equals(emp2)返回true),则hashCode也应该相同,所以应该重写hashCode。

	package com.java.day9_3;
	
	public class Test {
	
		public static void main(String[] args) {
			EMP emp1 = new EMP("hello");
			EMP emp2 = new EMP("hello");
			System.out.println(emp1.equals(emp2));
			System.out.println(emp1.hashCode());
			System.out.println(emp2.hashCode());
			
			String str1 = new String("hello");
			String str2 = new String("hello");
			System.out.println(str1.equals(str2));
			System.out.println(str1.hashCode());
			System.out.println(str2.hashCode());
		}
	
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值