JAVA Learning (7): Method overloading and method rewriting, this keyword and super keyword, and super keyword
Method overloading and method rewriting, this keyword, and super keyword
1. Method Overloading
Overload allows classes with the same name but different numbers and types of parameters to be passed to the method.
Note:
First, the parameter list of the overload method must be different from that of the overloaded method, and the difference must be clear enough to determine which method to call;
Second, the return value type of the overload method can be the same or different from that of the method to be overloaded. However, if the return value type is different, it cannot be considered as an overload.
For example, the most common println () method defines more than a dozen forms of overloading in JDK java. io. PrintStream. The common format is as follows:
Public void println (int I ){....}
Public void println (double I ){....}
Public void println (String I ){....}
/*************************************** * ******************* "Method overloading" and Its sample code */public class OverloadRewrite {void num (int i) {System. out. println ("received is an int type parameter whose value is =" + I);} void num (float j) {System. out. println ("received float type parameter, value =" + j);} void num (String k) {System. out. println ("received is a String type parameter whose value is =" + k);} public static void main (String [] args) {OverloadRewrite a = new OverloadRewrite (); a. num (100);. num (100.20f ); A. num ("Hello! ");}}
2. Method Rewriting
When a subclass inherits the parent class, it can have the member methods and member variables in the parent class and create a unique member in the subclass. However, if you create a method with the same name as the parent class, the same return value type, and the same parameter list, the implementation in the method body is different to implement the function different from that of the parent class, this method is called method override or method override.
Note:
First, the return value type and parameter list must be the same as the rewritten return value type and parameter list; otherwise, they cannot be called overwrites;
Second, the access modifier must be greater than the access modifier of the method to be overwritten (public> protected> default> private );
Third, the rewrite method cannot throw a new check exception or a more general check exception than the declared method. For example, a method of the parent class declares an IOException check. When you override this method, you cannot throw an Exception. You can only throw a subclass Exception of IOException, but can throw a non-check Exception.
/*************************************** * ******************* "Method rewriting" and Its sample code */public class OverloadRewrite {public String Name; public String Address; public OverloadRewrite (String name, String address) {// constructor this. name = name; this. address = address;} public String toString () {// rewrite toStringreturn "My Name is:" + Name + ", origin:" + Address ;} public class sichuancai extends OverloadRewrite {// inherits public String Detail; public sichuancai (String name, String address, String detail) {// constructor super (name, address ); // super calls the corresponding constructor of the parent class this. detail = detail ;}// public String toString () {// rewrite toString // return "My Name is:" + Name + ", origin: "+ Address +", features: "+ this. detail; //} public static void main (String [] args) {OverloadRewrite food = new OverloadRewrite ("noodle", "Italy"); System. out. println (food); sichuancai sichuan = food. new sichuancai ("Spicy", "Chengdu", "spicy and spicy"); System. out. println (sichuan );}}
3. this keyword
This keyword can be used to point to the current object in any instance method, or to the object that calls the current method for it, or to use it when the current type object reference is required.
Note: When the attribute name or member variable of a class is the same as the method parameter name used to access this property, you need to use this keyword to access the attribute of the class, parameters in the attributes and methods of the partition type.
4. super keyword
Because the subclass cannot inherit the constructor of the parent class, to call the constructor of the parent class, super () must be used in the first line of the constructor of the subclass ().
The super () method calls the corresponding constructor of the parent class to complete partial initialization of the subclass object.
Note: The program needs to use the super () keyword in the following two cases:
First, in the class constructor, the super statement is used to call the constructor of the class's parent class.
Second, access the members in the parent class in the subclass.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.