Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the ancestor class Object in Java

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian introduces in detail "how to use the ancestor class Object in Java", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the ancestor class Object class in Java" can help you solve your doubts.

Introduction of object class

Why do you say that object is the direct or indirect parent of all classes?

It can be seen that in such a class tree, the root of all classes is still the Object class.

Create a new class in IDEA and the system will inherit the Object class by default

Public class Pet extends Object {}

So Dog inherits the properties and behavior methods of the Pet class, but will it also inherit the properties and behavior methods of the Object class? This is certain, the Pet class is a subclass of the Object class, and the Dog class is a subclass of the Pet class, so Object is the indirect parent of the Dog class, and of course inherits the features of Object.

In other words, the Object class is the ancestor of all classes and the root of all classes, while Object translates to mean objects. Of course, it is a common saying of programmers: everything is an object.

That is, you can call the relevant properties and behavior methods of the Object class in the class:

For example, equals is one of them.

So which methods are often overridden by quilt subclasses in the Object class?

The method states that toString () returns information about the current object itself, compares whether two objects are the same object according to the string object return equals (), returns truehashCode () returns the hash code value getClass () of the object to get the class information to which the current object belongs, and returns the Class object

Next, take the Object method in the table as an example to briefly describe how to use it:

Equals () method

The equals () method of the Object class is used to compare whether two objects are the same object, and if so returns true, which is similar to the = = in the operator. In simple data types (int float double, etc.), compare values directly; in reference data types, compare whether they are the same object.

Package cn.zhz.Kind;// student class public class Student01 extends Object {private int sid; private String name; private int age; private int weight; public Student01 () {} public Student01 (int sid, String name, int age, int weight) {this.sid = sid; this.name = name; this.age = age; this.weight = weight;} public int getSid () {return sid } public void setSid (int sid) {this.sid = sid;} 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 int getWeight () {return weight } public void setWeight (int weight) {this.weight = weight;} public static void main (String [] args) {/ / student01 and student02 are not the same object Student01 student01 = new Student01 (1, "Zhang San", 12, 90); Student01 student02 = new Student01 (1, "Zhang San", 12, 90) / / determine whether two objects are the same object: = = or equals System.out.println (student01 = = student02); System.out.println (student01.equals (student02)); / / student03 and student04 are the same object Student01 student03 = new Student01 (2, "Zhao San", 22,450); Student01 student04 = student03; System.out.println (student03 = = student04) System.out.println (student03.equals (student04));}} override common methods in the Object class

Equals () in the Object class is used to determine whether two objects are the same object, so we require that two students whose student numbers and names are the same are the same object. In this case, we need to consider overriding the equals () method.

Override the equals method in the Object class

Package cn.zhz.Kind;import java.util.Objects;// student class public class Student01 extends Object {private int sid; private String name; private int age; private int weight; public Student01 () {} public Student01 (int sid, String name, int age, int weight) {this.sid = sid; this.name = name; this.age = age; this.weight = weight } public int getSid () {return sid;} public void setSid (int sid) {this.sid = sid;} 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 int getWeight () {return weight;} public void setWeight (int weight) {this.weight = weight } @ Override / / customize the equals () method body to our own comparison rule: if two students have the same student number and name, they are considered to be the same student object / / to determine whether obj and this (current student object) are the same object public boolean equals (Object obj) {if (this = = obj) {/ / if this's current student object and obj memory address are the same = = if the result is true, they are considered to be the same object return true } / / if the obj object passed in is not a student type, the result must be the false / / instanceof operator: used to determine whether an object is a certain type object name instanceof type name if (! (obj instanceof Student01)) {return false;} / / if obj is a Student01 type Student01 s = (Student01) obj / / compare sid and name of s and this to if (this.sid = = s.sid & & this.name.equals (s.name)) {return true;} else {return false;}} public static void main (String [] args) {/ / student01 and student02 are not the same object Student01 student01 = new Student01 (1, "Zhang San", 12, 90) Student01 student02 = new Student01 (1, "Zhang San", 12,90); / / determine whether two objects are the same object: = = or equals System.out.println (student01 = = student02); System.out.println (student01.equals (student02)); / / student03 and student04 are the same object Student01 student03 = new Student01 (2, "Zhao San", 22,450); Student01 student04 = student03 System.out.println (student03 = = student04); System.out.println (student03.equals (student04));}}

Instanceof is used to determine whether an object referenced by a reference type is an instance of a class.

Override the toString () method

Package cn.zhz.Kind;import java.util.Objects;// student class public class Student01 extends Object {private int sid; private String name; private int age; private int weight; public Student01 () {} public Student01 (int sid, String name, int age, int weight) {this.sid = sid; this.name = name; this.age = age; this.weight = weight } public int getSid () {return sid;} public void setSid (int sid) {this.sid = sid;} 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 int getWeight () {return weight;} public void setWeight (int weight) {this.weight = weight } @ Override / / customize the equals () method body to our own comparison rule: if two students have the same student number and name, they are considered to be the same student object / / to determine whether obj and this (current student object) are the same object public boolean equals (Object obj) {if (this = = obj) {/ / if this's current student object and obj memory address are the same = = if the result is true, they are considered to be the same object return true } / / if the obj object passed in is not a student type, the result must be the false / / instanceof operator: used to determine whether an object is a certain type object name instanceof type name if (! (obj instanceof Student01)) {return false;} / / if obj is a Student01 type Student01 s = (Student01) obj / / compare the sid and name of s and this to if (this.sid = = s.sid & & this.name.equals (s.name)) {return true;} else {return false;}} public String toString () {return this.name } public static void main (String [] args) {/ / student01 and student02 are not the same object Student01 student01 = new Student01 (1, "Zhang San", 12, 90); Student01 student02 = new Student01 (1, "Zhang San", 12, 90); / / determine whether two objects are the same object: = = or equals System.out.println (student01 = = student02) System.out.println (student01.equals (student02)); / / student03 and student04 are the same object Student01 student03 = new Student01 (2, "Zhao San", 22,450); Student01 student04 = student03; System.out.println (student03 = = student04); System.out.println (student03.equals (student04)); System.out.println ("-") / / the following two lines of code have the same effect. When you output an object, it is equivalent to calling its toString () method System.out.println (student01.toString ()); System.out.println (student01); String s = "test"; System.out.println (s.toString ()); System.out.println (s). } after reading this, the article "how to use the ancestral Object class in Java" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report