Overload (Overload):
* Method name is the same, parameter types are different or the number of parameters is different.
* There is no return value, the return value type and the return value are not the same without affecting the overload.
Example:
Public classSon { Public voidShowinta) {System.out.println (a); } Public voidShow (String s) {System.out.println (s); } Public voidShow (String s,inta) {System.out.println (a+s); } Public Static voidMain (string[] args) {son son=NewSon (); Son.show (10); Son.show ("Tim"); Son.show ("AED", 20); }}
Show Results:
Rewrite (overwrite/override):
Redefine the method of the parent class
--in subclasses can be overridden as needed from a method inherited from the parent class
--The overriding method must have the same method name as the overridden method, a parameter list, and a return type
--The overriding method cannot use more restrictive access than the overridden method
Override of ToString Method
System.out.println (s);
System.out.println (s.tostring);
Do not write. ToString is also the ToString output by default
ObjectThe method of the class toString returns a string consisting of the class name (the object is an instance of the class), the at marker " @ ", and an unsigned hexadecimal representation of the hash code for this object. In other words, the method returns a string whose value is equal to:
Before overriding ToString
After overriding ToString
Book.java
Public class Book { public String name; Public String Zuozhe; Public Book (String name,string zuozhe) { this. Name = name; this. Zuozhe = zuozhe; } Public String toString () { return "book [name=" + name + ", zuozhe=" + Zuozhe + "]"; }}
Main.java
Public class Main { publicstaticvoid main (string[] args) { =new HashMap (); new book ("Journey to the Monkey", "Wu Chengen"); System.out.println (D.tostring ());} }
Override of the Equals method
The address was compared before the rewrite.
You can define what you compare after you rewrite it.
Book.java
Public classBook { PublicString name; PublicString Zuozhe; PublicBook (String name,string zuozhe) { This. Name =name; This. Zuozhe =Zuozhe; } PublicString toString () {return"Book [name=" + name + ", zuozhe=" + Zuozhe + "]"; } Public Booleanequals (Object aa) {if(AAinstanceofBook ) {Book B=(book) AA; if( This. Name ==b.name && This. Zuozhe = =B.zuozhe) { return true; } } return false; }}
Main.java
Public class Main { publicstaticvoid main (string[] args) { =new HashMap (); New Book ("Journey to the Monkey", "Wu Chengen"); New Book ("Journey to the Monkey", "Wu Chengen"); Boolean bb = b.equals (d); System.out.println (BB); }}
Java Object-oriented (overloaded, overriding common tostring/equals)