常用类知识整理(一)

一、API文档

API文档: Application Promgramming Interface
如何阅读API文档?
1.看是在哪个包下
2.看是抽象类还是接口还是普通类

  1. 如果是抽象类,看子类

  2. 如果是接口,看实现类

  3. 如果是普通类,

    • 看构造方法
    • 看类的描述
    • 看构造方法
    • 看成员方法
  4. boolean equals(Object obj)

  5. protected Object clone()

  6. protected void finalize()

  7. Class<?> getClass()

  8. int hashCode()

  9. String toString()

  10. Object类的特点:

  11. .Object类是所有类的根类,可以利用多态创建对象

  12. 当一个方法的形参是Object类的时候,我们可以传入任意引用类型

  13. .Object满足万事万物皆对象的设计原则

  14. .创建任何一个类的对象都会去访问 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方法

hashCode方法:
com.sxt.objectdemo01.Student@7852e922 com.sxt.objectdemo01.Student@4e25154f com.sxt.objectdemo01.Student@70dea4e
false
false

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

三、字节码

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

四、String toString()

String toString()

  1. 观察源码:
    public String toString() {
    return s1.getClass().getName() + “@” + Integer.toHexString(2018699554);
    com.sxt.objectdemo01.Student@7852e922
    }

    默认输出一个对象,输出的是该对象 toString方法

    默认输出的是类名全路径+@+对象的hashCode的十六进制转换,毫无意义

    如果一个类的成员有100个,那么我一个一个重写很慢

    为什么输出一个对象输出的是toString方法
    s1
    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();
    }

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方法

equals方法

1.观察源码:
public boolean equals(Object obj) {
return (this == obj);
s1 == s2
s1 == s3
}

2.比较地址有意义吗?
按照道理应该比较对象的成员变量是否相等

3.模仿String的equals方法

class String {
private final char value[];

“abc”.equals(“abd”)
public boolean equals(Object anObject “abd”) {
// 提高程序的效率
if (this == anObject) { “abc” == “abd”
return true;
}
// 提高程序的安全性
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = this.value.length; // 3
if (n == anotherString.value.length 3) {
char v1[] = this.value; abc
char v2[] = anotherString.value; abd
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
}

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"));
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值