当前位置:网站首页>Method overloading and rewriting
Method overloading and rewriting
2022-07-03 12:44:00 【Persia_ Pepper】
Method overloading
What is method overloading ?
Method overloading refers to , You can define multiple methods with the same name , But the parameters are different .
Invocation time , The corresponding method will be automatically matched according to the different incoming parameters .
== Overloaded method , It's actually a completely different approach , It's just that the method name is the same ==
The conditions that make up method overloading
1. Different meanings : parameter types 、 Number of formal parameters 、 The order of formal parameters is different
2. Only the return value is different , Does not constitute an overload of a method
Such as :int a(String str){} And void a(String str){} Does not constitute a method overload
3. Only the names of formal parameters are different , Does not constitute an overload of a method
Such as : int a(String str){} And int a(String a){} Does not constitute a method overload
Code

summary
1. Method overloading : In the same class , Same method name , Multiple methods with different formal parameter lists , Call it method overloading
2. Dependency of method overloading : Method overloads are related to method names and formal parameters , And modifier and return Value type independent
3. Be careful : Unlike the formal parameter list, it refers to ?
(1) The number is different. :
add(); add(int a,int b); add(int a,int b,int c);
(2) Different order :
add(int a,double b); add(double a,int b);
(3) Different types :
add(int a); add(double a);
4. Make a simple overload judgment
(1)add(int a) and add (int b) ----> It doesn't constitute , It is equivalent to repeated definition of method , meaningless .
(2)public static int add(int a) and public static void add(int b)----> It doesn't constitute
expand

Method rewriting
The occurrence condition of rewriting
Occurs in subclasses and superclasses , When a child class inherits a parent class , And are not satisfied with the method provided by its parent class , Rewritable
Rewriting has strict format requirements
The method name of the subclass override must be the same as that of the parent , And with the parameter list in the parent class method ( Number 、 type 、 The order ) bring into correspondence with !
public class Person {
// Parent class -- human beings
public void eat(){
System.out.println(" I like to eat pig elbows ...");
}
public void sleep(){
System.out.println(" I like sleeping ");
}
}
public class Student extends Person{
// Subclass -- Student
public void study(){
System.out.println(" I love learning ...");
}
@Override // Overload comment
public void eat(){
System.out.println(" I love string ...");
}
}
public class Student extends Person{
// Subclass -- Student
public void study(){
System.out.println(" I love learning ...");
}
@Override // Overload comment
public void eat(){
System.out.println(" I love string ...");
}
}
public static void main(String[] args) {
Student st = new Student();
st.eat();
st.sleep();
}
Running results :
I love string ...
I like sleeping
Memory analysis

The difference between overloading and rewriting
heavy load : In the same class , Same method name , Multiple methods with different formal parameter lists , Constitute heavy load
rewrite : In different classes , When the subclass inherits the parent method and is not satisfied , Rewrite its method

边栏推荐
- 并网-低电压穿越与孤岛并存分析
- Dix règles de travail
- Sword finger offer10- I. Fibonacci sequence
- Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
- Sword finger offer07 Rebuild binary tree
- Do you feel like you've learned something and forgotten it?
- CNN MNIST handwriting recognition
- 【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
- lambda与匿名内部类的区别
- 剑指Offer07. 重建二叉树
猜你喜欢

2.7 overview of livedata knowledge points

Self made pop-up input box, input text, and click to complete the event.
![[ManageEngine] the role of IP address scanning](/img/dc/df353da0e93e4d936c39a39493b508.png)
[ManageEngine] the role of IP address scanning

Attack and defense world mobile--ph0en1x-100

Day 1 of kotlin learning: simple built-in types of kotlin

Swift bit operation exercise

剑指Offer03. 数组中重复的数字【简单】

Record your vulnhub breakthrough record

LeetCode 0556. Next bigger element III - end of step 4

电压环对 PFC 系统性能影响分析
随机推荐
1-1 token
Sword finger offer07 Rebuild binary tree
基于Linu开发的项目视频
Do you feel like you've learned something and forgotten it?
[ManageEngine] the role of IP address scanning
Eureka self protection
The solution to change the USB flash disk into a space of only 2m
RedHat5 安装Socket5代理服务器
Swift Error Handling
257. All paths of binary tree
init. RC service failed to start
Lambda expression
【嵌入式】---- 内存四区介绍
Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.
剑指Offer10- I. 斐波那契数列
LeetCode 0556. Next bigger element III - end of step 4
ImportError: No module named examples. tutorials. mnist
强大的头像制作神器微信小程序
Enter the length of three sides of the triangle through the user, and calculate the area of the triangle, where the length is a real number
ORM use of node -serialize