オブジェクトクラス構造(一般的に使用されるクラス)の構造(オブジェクト指向の非メインラインコンテンツ)

java.lang.Object类

1. Objectクラスは、すべてのJavaクラスのルート親クラスである。
2.キーワード拡張し、その親クラスを示すために、クラスの宣言に使用されていない場合は、デフォルトの親クラスはjava.lang.Objectクラスで
3オブジェクトクラス関数(属性、メソッド)はユニバーサルです。

属性:无

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

4. Objectクラスは、nullパラメーターを持つコンストラクターのみを宣言します

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

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

==:演算子(基本データ型変数および参照データ型変数で使用できます)

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

补充: == 符号使用时,必须保证符号左右两边的变量类型一致。
我们对以上内容实际测试如下:
引用类型:
		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

equals()メソッドの使用

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;
	}

ここに画像の説明を挿入

	重写的原则:比较两个对象的实体内容(即: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.オブジェクトへの参照を出力するとき、実際には現在のオブジェクトのtoString()を呼び出します。

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. ObjectクラスでのtoString()の定義:

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

3. String、Date、File、包装类等ObjectクラスのtoString()メソッドを書き直したいと思います。
オブジェクトのtoString()を呼び出すときに、「エンティティコンテンツ」情報を返すようにします

4、カスタムクラスはtoString()メソッドをオーバーライドすることもできます、このメソッドが呼び出されると、オブジェクトの「エンティティコンテンツ」が返されます

手动实现

@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ダウンロードリンク:https:
//mvnrepository.com/artifact/org.hamcrest/hamcrest-core/1.3 43KBjarパッケージをダウンロードするだけです

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

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

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

包装类(Wrapper)的使用

1.Javaは提供します8つの基本データ型に対応するパッケージングクラス、これにより、基本データ型の変数はクラスの特性を持ちます
ここに画像の説明を挿入

概要:基本型、パッケージングクラス、文字列クラス
2間の変換基本的なデータ型、パッケージタイプ、および文字列の間相互変換

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

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);
	}

面接の質問

	@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
	}

おすすめ

転載: blog.csdn.net/AC_872767407/article/details/113615066