Anatomy of the Object class structure (commonly used classes) (object-oriented non-mainline content)

java.lang.Object类

1. The Object class is the root parent class of all Java classes.
2. If the extends keyword is not used in the declaration of the class to indicate its parent class, the default parent class is java.lang.Object class
3. Object class function (attribute , Method) is universal.

属性:无

方法:equals() / toString() 
	 getClass() 
	 hashCode() 
	 clone() / finalize()
	 wait() 、 notify()、notifyAll()

4. The Object class only declares a constructor with null parameters

Order order = new Order();
System.out.println(order.getClass().getSuperclass());
面试题:final、finally、finalize的区别?

面试题: == 和 equals() 区别

==: Operator (can be used in basic data type variables and reference data type variables)

如果比较的是基本数据类型变量:比较两个变量保存的数据是否相等。(不一定类型要相同)(自动类型提升)
如果比较的是引用数据类型变量:比较两个对象的地址值是否相同.即两个引用是否指向同一个对象实体

补充: == 符号使用时,必须保证符号左右两边的变量类型一致。
我们对以上内容实际测试如下:
引用类型:
		Customer cust1 = new Customer("Tom",21);
		Customer cust2 = new Customer("Tom",21);
		System.out.println(cust1 == cust2);//false
		
		String str1 = new String("atguigu");
		String str2 = new String("atguigu");
		System.out.println(str1 == str2);//false

Use of equals() method

1. 是一个方法,而非运算符,只能适用于引用数据类型。
2. Object类中equals()的定义:
		public boolean equals(Object obj) {
    
    
	        	return (this == obj);
		}
说明:Object类中定义的equals()==的作用是相同的:比较两个对象的地址值是否相同.即两个引用是否指向同一个对象实体

3.像String、Date、File、包装类等都重写了Object类中的equals()方法。
重写以后,比较的不是两个引用的地址是否相同,而是比较两个对象的"实体内容"是否相同。

4.通常情况下,我们自定义的类如果使用equals()的话,也通常是比较两个对象的"实体内容"是否相同。那么,我们就需要对Object类中equals()进行重写.

重写的原则:比较两个对象的实体内容是否相同.

================================================================

equals()方法:
		System.out.println(cust1.equals(cust2));//false--->true 多态无处不在(形参object,这里是其子类)
		System.out.println(str1.equals(str2));//true   重写了Object类中的equals()方法
1、自动生成的equals()     目前已经掌握了------>生成构造方法、生成get()和set()方法、生成equals()和hash()
2、手动实现equals()的重写
	@Override  //自动生成的equals()
	public boolean equals(Object obj) {
    
      //Object obj  多态无处不在
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
			
		//正式比较
		Customer other = (Customer) obj;
		if (age != other.age)
			return false;
		if (name == null) {
    
    
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

Insert picture description here

	重写的原则:比较两个对象的实体内容(即:name和age)是否相同
	手动实现equals()的重写
	
	@Override
	public boolean equals(Object obj) {
    
    

//		System.out.println("Customer equals()....重写的方法执行了");
		if (this == obj) {
    
    
            return true;
        }

		if(obj instanceof Customer){
    
    
			Customer cust = (Customer)obj;
			//比较两个对象的每个属性是否都相同
//			if(this.age == cust.age && this.name.equals(cust.name)){
    
    
//				return true;
//			}else{
    
    
//				return false;
//			}

			//或
			return this.age == cust.age && this.name.equals(cust.name);
			//基本数据类型age只能用==    引用数据类型name只能用equals()方法
		}else{
    
    
			return false;

		}

	}

Object类中toString()的使用

1. When we output a reference to an object, we actually call toString() of the current object

Customer cust1 = new Customer("Tom",21);	//com.atguigu.java1.Customer@15db9742
System.out.println(cust1.toString());		//com.atguigu.java1.Customer@15db9742
System.out.println(cust1);

2. The definition of toString() in the Object class:

public String toString() {
    
    
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

3. Like String、Date、File、包装类等to rewrite the toString() method in the Object class.
Make it return "entity content" information when calling toString() of the object

4、Custom classes can also override the toString() method, When this method is called, the "entity content" of the object is returned

手动实现

@Override
public String toString() {
    
    
	return "Customer[name = " + name + ",age = " + age + "]"; 
}

自动实现
@Override
public String toString() {
    
    
	return "Customer [name=" + name + ", age=" + age + "]";
}

Java中的JUnit单元测试

hamcrest-core-1.3.jar download link: https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core/1.3 just
download the 43KB jar package

此单元测试方法上需要声明注解:@Test,并在单元测试类中导入:import org.junit.Test;

声明好单元测试方法以后,就可以在方法体内测试相关的代码。

1.如果执行结果没有任何异常:绿条
2.如果执行结果出现异常:红条

包装类(Wrapper)的使用

1. Java providesPackaging classes corresponding to 8 basic data types, Which makes the variables of the basic data type have the characteristics of the class
Insert picture description here

Summary: Conversion between basic types, packaging classes and String classes
2,Between basic data types, packaging types, and StringMutual conversion

总结:(只看这个就可以了)

1、JDK 5.0 新特性:自动装箱 与自动拆箱
		int num2 = 10;
		Integer in1 = num2;//自动装箱
		int num3 = in1;//自动拆箱
		
2、基本数据类型、包装类--->String类型:调用String重载的valueOf(Xxx xxx)
		float f1 = 12.3f;
		String str2 = String.valueOf(f1);//"12.3"
		
3、String类型 --->基本数据类型、包装类:调用包装类的parseXxx(String s)
		String str1 = "123";
		int num2 = Integer.parseInt(str1);
		System.out.println(num2 + 1);  //124
======================================================================================================
基本数据类型、包装类--->String类型:调用String重载的valueOf(Xxx xxx)

	@Test
	public void test4(){
    
    
		
		int num1 = 10;
		//方式1:连接运算
		String str1 = num1 + "";
		//方式2:调用String的valueOf(Xxx xxx)
		float f1 = 12.3f;
		String str2 = String.valueOf(f1);//"12.3"
		
		Double d1 = new Double(12.4);
		String str3 = String.valueOf(d1);
		System.out.println(str2);
		System.out.println(str3);//"12.4"
		
	}
	
String类型 --->基本数据类型、包装类:调用包装类的parseXxx(String s)
	@Test
	public void test5(){
    
    
		String str1 = "123";
		//错误的情况:
//		int num1 = (int)str1;
//		Integer in1 = (Integer)str1;
		//可能会报NumberFormatException
		int num2 = Integer.parseInt(str1);
		System.out.println(num2 + 1);
		
		String str2 = "true1";
		boolean b1 = Boolean.parseBoolean(str2);
		System.out.println(b1);
	}
JDK 5.0 新特性:自动装箱 与自动拆箱

	@Test
	public void test3(){
    
    
//		int num1 = 10;
//		//基本数据类型-->包装类的对象
//		method(num1);
		
		//自动装箱:基本数据类型 --->包装类
		int num2 = 10;
		Integer in1 = num2;//自动装箱
		
		boolean b1 = true;
		Boolean b2 = b1;//自动装箱
		
		//自动拆箱:包装类--->基本数据类型
		System.out.println(in1.toString());
		int num3 = in1;//自动拆箱	
	}
	
	public void method(Object obj){
    
    
		System.out.println(obj);
	}

基本数据类型 --->包装类:调用包装类的构造器

		int num1 = 10;
//		System.out.println(num1.toString());   error
		Integer in1 = new Integer(num1);
		System.out.println(in1.toString());  //10
		
		Integer in2 = new Integer("123");
		System.out.println(in2.toString());   //123
		//报异常		NumberFormatException: For input string: "123abc"
//		Integer in3 = new Integer("123abc");
//		System.out.println(in3.toString());
//=======================================================
		Float f1 = new Float(12.3f);
		Float f2 = new Float("12.3");
		System.out.println(f1);  //12.3
		System.out.println(f2);  //12.3
//=======================================================
		Boolean b1 = new Boolean(true);
		Boolean b2 = new Boolean("TrUe");
		System.out.println(b2);   //true
		Boolean b3 = new Boolean("true123");
		System.out.println(b3);//false	    布尔类型特别一点:优化过
包装类--->基本数据类型:调用包装类Xxx的xxxValue()

	@Test
	public void test2(){
    
    
		Integer in1 = new Integer(12);
		
		int i1 = in1.intValue();
		System.out.println(i1 + 1);
		
		
		Float f1 = new Float(12.3);
		float f2 = f1.floatValue();
		System.out.println(f2 + 1);
	}

Interview questions

	@Test
	public void test1() {
    
    
		Object o1 = true ? new Integer(1) : new Double(2.0);
		System.out.println(o1);// 1.0

	}
	
	@Test    //为什么??
	public void test2() {
    
    
		Object o2;
		if (true)
			o2 = new Integer(1);
		else
			o2 = new Double(2.0);
		System.out.println(o2);// 1

	}
	
	@Test
	public void test3() {
    
    
		Integer i = new Integer(1);
		Integer j = new Integer(1);
		System.out.println(i == j);//false
	
		
Integer内部定义了IntegerCache结构(内部类),IntegerCache中定义了Integer[],
保存了从-128~127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在-128~127范围内时,
可以直接使用数组中的元素,不用再去new了。
目的:提高效率
		@Test
		Integer m = 1;
		Integer n = 1;
		System.out.println(m == n);//true

		Integer x = 128;//相当于new了一个Integer对象
		Integer y = 128;//相当于new了一个Integer对象
		System.out.println(x == y);//false
	}

Guess you like

Origin blog.csdn.net/AC_872767407/article/details/113615066