当前位置:网站首页>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
边栏推荐
- If you can't learn, you have to learn. Jetpack compose writes an im app (I)
- Implement verification code verification
- The best shortcut is no shortcut
- Tensorflow binary installation & Failure
- Attack and defense world mobile--ph0en1x-100
- Project video based on Linu development
- OpenStack节点地址改变
- temp
- 剑指Offer05. 替换空格
- 【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
猜你喜欢
阿里大于发送短信(用户微服务--消息微服务)
Self made pop-up input box, input text, and click to complete the event.
2.7 overview of livedata knowledge points
Solve the problem of VI opening files with ^m at the end
Xctf mobile--app3 problem solving
ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍
Eureka自我保护
Implement verification code verification
Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
Low code platform international multilingual (I18N) technical solution
随机推荐
node的ORM使用-Sequelize
1-2 project technology selection and structure
02_ Lock the code, and don't let the "lock" become a worry
adb push apk
Use bloc to build a page instance of shutter
I'm too lazy to write more than one character
flinksql是可以直接客户端建表读mysql或是kafka数据,但是怎么让它自动流转计算起来呢?
T430 toss and install OS majave 10.14
Display time with message interval of more than 1 minute in wechat applet discussion area
2.7 overview of livedata knowledge points
最新版抽奖盲盒运营版
Unicode查询的官方网站
Tensorflow binary installation & Failure
Cloud Computing future - native Cloud
Swift5.7 extend some to generic parameters
Write a simple nodejs script
Low code platform international multilingual (I18N) technical solution
Adult adult adult
Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment