Java APIObject类的常用API文档
admin
2024-02-23 11:14:38

文章目录

文章目录

      • 文章目录
    • Java常用[API](https://so.csdn.net/so/search?q=API&spm=1001.2101.3001.7020)介绍
      • API概念
      • Object类
        • toString方法
        • equals方法

Java常用API介绍

API概念

什么是API?

API(Application Programming interface) 应用程序编程接口。

简单来说:就是Java帮我们已经写好的一些方法**,我们直接拿过来用就可以了。**

Object类

Object类的作用:

  • Object类的方法是一切子类对象都可以直接使用的,所以我们要学习Object类的方法。
  • 一个类要么默认继承了Object类,要么间接继承了Object类,Object类是Java中的祖宗类

toString方法

方法名说明
toString()默认是返回当前对象在堆内存中的地址信息: 类的全限名@内存地址

基本使用:

例如有一个Student类

public class Test {public static void main(String[] args) {Student stu = new Student();// 方式一: 将返回的地址在变量中接收String str = stu.toString();System.out.println(str);// 方式二: 输出时, 直接调用toString方法System.out.println(stu.toString());// 方式三: 直接输出对象变量, 默认可以省略toString方法System.out.println(stu);}
}

问题引出:

在开发中直接输出对象变量,默认输出对象的地址其实是毫无意义的。

事实上, 开发中更多的时候是希望看到对象的内容数据而不是对象的地址信息。

toString存在的意义:

父类toString()方法存在的意义是为了被子类重写,以便返回对象的内容信息,而不是地址信息!!

演示代码:

例如我们有如下一个Student类

package com.chenyq.d12_api_object;/**默认继承自Object*/
public class Student {private String name;private int age;private double height;public Student() {}public Student(String name, int age, double height) {this.name = name;this.age = age;this.height = height;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}
}

我们可以在该Student类中重写toString方法

package com.chenyq.d12_api_object;/**默认继承自Object*/
public class Student {private String name;private int age;private double height;public Student() {}public Student(String name, int age, double height) {this.name = name;this.age = age;this.height = height;}// 重新toString方法@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", height=" + height +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}
}

此时再调用Student对象变量的toString方法, 会调用我们自己重写的toString, 返回对象的内容

public class Test {public static void main(String[] args) {Student stu = new Student("chenyq", 18, 1.88);System.out.println(stu); // Student{name='chenyq, age=18, height=1.88}}
}

小结:

Object的toString方法的作用是什么?

  • 让子类重写toString方法,以便返回子类对象中的内容。

equals方法

方法名说明
equals(Object o)默认是比较当前对象与另一个对象的地址是否相同,相同返回true,不同返回false

基本使用:

我们还是继续使用上面的Student类演示

public class Test2 {public static void main(String[] args) {Student stu1 = new Student("chenyq", 18, 1.88);Student stu2 = new Student("chenyq", 18, 1.88);// 这里equals调用的是Object中的System.out.println(stu1.equals(stu2)); // false}
}

问题思考:

直接比较两个对象的地址是否相同完全可以用 “==”, 那么为什么要使用equals呢?

equals存在的意义

在开发中, 其实我们并不会使用equals方法比较两个对象的地址, 通常是用来比较两个对象内容是否相同

而父类equals方法存在的意义就是为了被子类重写,以便子类自己来定制比较规则。

演示代码:

重写equals方法, 自己定制比较规则: 例如比较两个对象内容是否相同

package com.chenyq.d12_api_object;import java.util.Objects;/**默认继承自Object*/
public class Student {private String name;private int age;private double height;public Student() {}public Student(String name, int age, double height) {this.name = name;this.age = age;this.height = height;}/**重写equals方法s1 -> thiss2 -> o*/@Overridepublic boolean equals(Object o) {// 1. 判断this和o是否是同一个对象if (this == o) return true;// 2. 判断o是否为null以及this和o类型是否相同if (o == null || getClass() != o.getClass()) return false;// 3. 将o强转为Student类型Student student = (Student) o;// 4. 返回age, height, name的比较结果return age == student.age && Double.compare(student.height, height) == 0 && Objects.equals(name, student.name);public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}
}

此时再调用Student对象的equals方法, 就是调用的自己重写的equals

public class Test2 {public static void main(String[] args) {Student stu1 = new Student("chenyq", 18, 1.88);Student stu2 = new Student("chenyq", 18, 1.88);// 这里equals调用的是自己重写的, 比较对象内容是否相同System.out.println(stu1.equals(stu2)); // true}
}

相关内容

热门资讯

复刻贵州馆子味!家常泡椒炒牛肉... 贵州泡椒炒牛肉是一道充满地方特色的家常菜,它以鲜嫩的牛肉和酸辣开胃的泡椒为主要食材,成菜香气扑鼻,口...
黔寨风味“黄金派”:外酥内糯,... 在贵州连绵的群山与缭绕的云雾间,散落着许多古老村寨。这里不仅保留着深厚的民族传统,更隐藏着无数令人惊...
大妈教你东北芥菜疙瘩的腌制方法... 眼下正是腌菜的好时节,每年这个时候,我总会想起东北大娘腌的芥菜疙瘩,那味道堪称一绝。她的做法特别简单...
原创 一... 家人们谁懂啊!黑椒牛肉配杏鲍菇真的是神仙组合!软嫩多汁的牛肉裹着浓郁的黑椒酱汁,杏鲍菇吸饱了肉香变得...