Home> Java> javaTutorial> body text

How to override method in java?

coldplay.xixi
Release: 2020-06-24 15:10:46
Original
5895 people have browsed it

Methods for method rewriting: 1. [toString()] method, returns an object in the form of a string; 2. [equals()] method, compares whether the references of two objects are equal, code It is [sq s2=new sq(5,4);println(s1.equals(s2)].

How to override method in java?

Rewritten by the method in java Method:

The rewriting of methods in Java is based on one of the three major characteristics of the Java class: inheritance. Without inheritance, we cannot talk about method rewriting. Method rewriting is an operation in which when a method of the parent class in the program cannot meet the needs of the subclass, the subclass can redefine the content and function of the method to meet the needs of the subclass. So rewriting of methods Specifically how to implement it through code, the blogger below will take you to find out.

(1) Define a polygon class

class Polygon{ //属性 private int number_side; //构造器 public Polygon(int number_side) { super(); this.number_side = number_side; } //方法 public int getNumber_side() { return number_side; } public void setNumber_side(int number_side) { this.number_side = number_side; } public void show(){ System.out.println("Number_side is " + this.number_side); } }
Copy after login

In this class, in addition to the get and set methods In addition, there is also a show method that can output the number of polygon sides.

(2) Define a square class inherited from the polygon class

class square extends Polygon{ //属性 private double length; //构造器 public square(double length, int number_side) { super(number_side); this.length = length; } //方法 public double getLength() { return length; } public void setLength(double length) { this.length = length; } //输出边数和边长 public void show(){ System.out.println("This is a square"); super.show(); System.out.println("Length is " + this.length); } }
Copy after login

You can see that there is still a show in the subclass square method, but the function and statement of the method are quite different from the show method in its parent class. Because, in the subclass square, the function of the show method of the subclass square must not only be able to output the number of sides, but also the length of the sides output, so the show method of the parent class cannot meet the needs of the subclass at this time. The developer should rewrite a show method to meet the needs of the subclass. This is method rewriting in java.

In the actual development process, there are many other situations where method rewriting is applied. Next, this article will list several more commonly used method rewriting.

  Under the java.lang package of java There is a class named Object. Object is a special class, which is the parent class of all classes. When we create a class, if we do not declare it to inherit from the class we created ourselves, then it will inherit from Object, only However, the extends Object keyword is omitted in java. There are two frequently used methods in the Object class: 1.toString() method; 2.equals() method. These two methods are often repeated in classes created by developers. Write.

1. toString() method

The function of toString()method is to return an object in the form of a string. For example :

Polygon p = new Polygon(3); System.out.println(p.toString());
Copy after login

ThetoString()method called here is thetoString()method in the Object class.

How to override method in java?

The output is:

How to override method in java?

#It can be seen that when thetoString()method in the Object class is called, an object in the form of a string is returned. , that is, the address of the object.

In actual applications, thetoString()method is usually overridden to provide a specific string output mode for the object, for example:

public class Test { public static void main(String[] args) { Polygon p = new Polygon(3); System.out.println(p.toString()); } } class Polygon{ //属性 private int number_side; //构造器 public Polygon(int number_side) { super(); this.number_side = number_side; } //..................................此处省略其他无关的方法 @Override public String toString() { return "Polygon [number_side=" + number_side + "]";
Copy after login

In the polygon class Polygon ThetoString()method has been rewritten. In the main method, we create a Polygon object p and instantiate it, and call the rewrittentoString()method in Polygon.

How to override method in java?

At this time, the system outputs the Polygon class name and its attributes in string form.

How to override method in java?

2. The equals() method

The specific embodiment of the equals()method in the Object class What is it like? What is its function? The old rule is to go directly to the code.

public boolean equals(Object obj) { return (this == obj); }
Copy after login

This is the specific implementation of theequals()method of the Object class in the source code, so we know that the function of theequals()method in Object is to compare Whether the references of two objects are equal. When we call theequals()method in the Object class:

public class Test { public static void main(String[] args) { square s1 = new square(5.2,4); square s2 = new square(5.2,4); System.out.println(s1.equals(s2)); } }
Copy after login
Copy after login

The output of the system is:

How to override method in java?

Then we rewriteequals()method in the square class

public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; square other = (square) obj; if (Double.doubleToLongBits(length) != Double .doubleToLongBits(other.length)) return false; return true; }
Copy after login

When theequals()method in the square class is called again

public class Test { public static void main(String[] args) { square s1 = new square(5.2,4); square s2 = new square(5.2,4); System.out.println(s1.equals(s2)); } }
Copy after login
Copy after login

the output of the system is:

How to override method in java?

Compared with the previous fasle, the output of true at this time is because theequals()method is rewritten, and the rewrittenequals( )The method compares the actual contents of the two objects, that is, the attributes of the two objects (note: the equals() method does not compare the methods of the two objects because it is meaningless), and outputs true if they are equal.

The above is the basic knowledge about method rewriting and some common points. The blogger mentioned before in the chapter on polymorphism: method rewriting is also a manifestation of polymorphism. Now we can know that they are alsotoString()andequals ()method, after being rewritten in a custom class, has completely different functions from the Object class. This is also a different manifestation of the same thing, which is in line with the nature of polymorphism.

Recommended tutorial: "java video tutorial"

The above is the detailed content of How to override method in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!