一、Object
1.含义
所有类的基类/超类,一个类没有明确继承的类,默认继承Object
//默认继承Object
public class A{
String str1;
String str2;
int i;
private void method01(){}
public void method02(){
method01();
}
}
public class Test01 {
public static void main(String[] args) {
/**
* 知识点:Object
* 含义:所有类的基类/超类,一个类没有明确继承的类,默认继承Object
*
* equals:比较两个对象内存地址是否相同
* public boolean equals(Object obj){
* return this == obj;
* }
* getClass:
* hashCode:
* toString:
*/
Object obj1 = new Object();//0x001
Object obj2 = new Object();//0x002
boolean equals = obj1.equals(obj2);
System.out.println(equals);//false
boolean bool = obj1 == obj2;
System.out.println(bool);//false
}
}
2.equals
比较两个对象内存地址是否相同
* public boolean equals(Object obj){
* return this == obj;
* }
3.getClass
获取该类的字节码文件对象
public class Test02 {
public static void main(String[] args) {
/**
* 知识点:Object
* 含义:所有类的基类/超类,一个类没有明确继承的类,默认继承Object
*
* equals:比较两个对象内存地址是否相同
* public boolean equals(Object obj){
* return this == obj;
* }
* getClass:获取该类的字节码文件对象
* hashCode:
* toString:
*/
Object obj1 = new Object();//0x001
Object obj2 = new Object();//0x002
Class<? extends Object> c1 = obj1.getClass();
Class<? extends Object> c2 = obj2.getClass();
System.out.println(c1 == c2);//true
}
}
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test03 {
public static void main(String[] args) {
/**
* 知识点:Object
* 含义:所有类的基类/超类,一个类没有明确继承的类,默认继承Object
*
* equals:比较两个对象内存地址是否相同
* public boolean equals(Object obj){
* return this == obj;
* }
* getClass:获取该类的字节码文件对象
* hashCode:
* toString:
*/
A a = new A();
//获取A类的字节码文件对象
Class<? extends A> c = a.getClass();
//获取A类中所有属性
Field[] declaredFields = c.getDeclaredFields();
for (Field field : declaredFields) {
System.out.println(field);
}
//获取A类中所有的方法
Method[] declaredMethods = c.getDeclaredMethods();
for (Method method : declaredMethods) {
System.out.println(method);
}
}
}
4.hashCode
获取对象的hash值
* hash值-系统利用对象的内存地址+散列算法获取的一个值
* 注意:hash值不等于内存地址
public class Test04 {
public static void main(String[] args) {
/**
* 知识点:Object
* 含义:所有类的基类/超类,一个类没有明确继承的类,默认继承Object
*
* equals:比较两个对象内存地址是否相同
* public boolean equals(Object obj){
* return this == obj;
* }
* getClass:获取该类的字节码文件对象
* hashCode:获取对象的hash值
* hash值-系统利用对象的内存地址+散列算法获取的一个值
* 注意:hash值不等于内存地址
* toString:
*/
Object obj1 = new Object();
Object obj2 = new Object();
System.out.println(obj1.hashCode());
System.out.println(obj2.hashCode());
}
}
5.toString
获取对象的字符串表示(将对象转换为字符串)
* public String toString(){
* return getClass().getName() + “@” + Integer.toHexString(hashCode());
* }
public class Test05 {
public static void main(String[] args) {
/**
* 知识点:Object
* 含义:所有类的基类/超类,一个类没有明确继承的类,默认继承Object
*
* equals:比较两个对象内存地址是否相同
* public boolean equals(Object obj){
* return this == obj;
* }
* getClass:获取该类的字节码文件对象
*
* hashCode:获取对象的hash值
* hash值-系统利用对象的内存地址+散列算法获取的一个值
* 注意:hash值不等于内存地址
*
* toString:获取对象的字符串表示(将对象转换为字符串)
* public String toString(){
* return getClass().getName() + "@" + Integer.toHexString(hashCode());
* }
*/
Object obj1 = new Object();
//java.lang.Object@15db9742
System.out.println(obj1.toString());
}
}
二、Object标杆作用
1.创建演示类
public class Student {
private String name;
private char sex;
private int age;
private String classId;
private String id;
public Student() {
}
public Student(String name, char sex, int age, String classId, String id) {
this.name = name;
this.sex = sex;
this.age = age;
this.classId = classId;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
Student stu = (Student) obj;
if(this.classId.equals(stu.classId) && this.id.equals(stu.id)){
return true;
}
return false;
}
@Override
public String toString() {
return "name=" + name + ", sex=" + sex + ", age=" + age + ", classId=" + classId + ", id=" + id;
}
}
public class User {
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
User user = (User) obj;
if(this.username.equals(user.username)){
return true;
}
return false;
}
@Override
public String toString() {
return username + " -- " + password;
}
}
2.equals
判断两个对象是否相同,Object不同的子类判断两个的对象是否相同有不同的判断规则,子类重写equals即可
3.toString
返回对象的字符串表示,Object不同的子类有不同的属性,重写toString,返回属性
public class Test01 {
public static void main(String[] args) {
/**
* 知识点:Object标杆作用
*
* equals:判断两个对象是否相同,Object不同的子类判断两个的对象是否相同有不同的判断规则,子类重写equals即可
* toString:返回对象的字符串表示,Object不同的子类有不同的属性,重写toString,返回属性
*/
User user1 = new User("1445584980", "123123");
User user2 = new User("1445584980", "123123");
System.out.println(user1.equals(user2));//true
System.out.println(user1);//默认调用toString
}
}