Java常用核心类之终极父类Object

Posted nuist__NJUPT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java常用核心类之终极父类Object相关的知识,希望对你有一定的参考价值。

Java常用核心类之终极父类Object

  • java.lang.Object类是Java中所有类的父类
  • 下面介绍几个Object类的常用方法
  • 1.toString()
  • 2.equals()
  • 3.hashCode()
  • 4.clone()
  • 5.finalize()
import java.util.Objects;
public class Employee {
    private String name ;
    private int age ;
    private double salary ;
    private int id ;

    public Employee(int id, String name, int age, double salary){
        this.id = id ;
        this.name = name ;
        this.age = age ;
        this.salary = salary ;
    }
    @Override
    public String toString(){ //在Employee类中重写toString()方法,使其返回一个有意义的字符串
        return "员工信息:" + name + " " + age + " " + salary ;
    }
    @Override
    public boolean equals(Object object){ //重写equals方法,判断id是否相等
        if(object instanceof Employee){ //判断是否属于员工类的实例化对象
            return this.id == ((Employee)object).id ;
        }
        else{
            return false ;
        }
    }
    @Override
    public int hashCode(){
        return Objects.hash(name,age,salary) ;
    }
    public static void main(String[] args){
        Employee employee = new Employee(46,"王国栋",24,5000) ;
        System.out.println(employee.toString()) ;
        System.out.println(employee) ; //省略toString(),Java编译器将自动调用toString()方法
        Employee emp1 = new Employee(11,"唐乃乔",24,5000) ;
        Employee emp2 = new Employee(11,"唐乃乔",24,5000) ;
        System.out.println(emp1.equals(emp2)) ;  //比较两个对象的id是否相等
        //hashCode()方法返回一个对象的哈希码值,
        System.out.println(employee.hashCode()) ;

    }
}

  • 使用Object类的clone()方法可以克隆一个对象,即为对象创建一个副本
  • 要使得对象能够克隆,类必须实现Cloneable接口
  • 在Java程序中,每一个对象都有一个finalize()方法,在对象被销毁之前,垃圾回收器允许,调用改方法进行清理工作
public class Car implements Cloneable{ //类必须实现Cloneable接口
    private int id ; //编号
    private String brand ; //品牌
    private String color ; //颜色
    public Car(int id, String brand, String color){
        this.id = id ;
        this.brand = brand ;
        this.color = color ;
    }
    @Override
    public boolean equals(Object object){ //重写equals方法
       return  this.id == ((Car)object).id ;
    }
    @Override
    public String toString() {
        return "汽车:id = " + id + " brand = " + brand + "color = " + color;
    }
    @Override
    protected void finalize() throws Throwable{
        System.out.println("The object is destroyed") ;
    }

    public static void main(String[] args) throws CloneNotSupportedException { //需要抛储不支持克隆异常
        Car car1 = new Car(101,"宝马","黑色") ;
        Car car2 =(Car)car1.clone() ;
        System.out.println(car1 == car2) ; //比较car1和car2的引用是否相同
        System.out.println(car1.equals(car2)) ; //比较car1与car2的id是否相同
        System.out.println(car1.getClass().getName()) ; //car1的类名
        System.out.println(car1.hashCode()) ; //哈希码
        System.out.println(car1.toString()) ; //也可以将toString()省略
        Car car3 = new Car(102,"奔驰", "蓝色") ;
        car2 = null ;
        car3 = null ;
        System.gc() ; //执行垃圾回收
    }
}

以上是关于Java常用核心类之终极父类Object的主要内容,如果未能解决你的问题,请参考以下文章

java视频传智笔记------Object

object类

Java全栈JavaSE:17.常用类之Object类String类StringBuilder类

11.JAVA-Object类之finalize(),clone(),toString()等方法覆写

day14

Java之面向对象Object类