当前位置:网站首页>Method overloading and method rewriting
Method overloading and method rewriting
2022-07-26 21:49:00 【Bao Zhou Pao】
Catalog
Two 、 Method rewriting ( Methods cover )
3、 ... and 、 Comparison between method overloading and method rewriting
One 、 Method overloading
Java Allow a class , Multiple methods with the same name exist , But the formal parameter list is required to be inconsistent. Multiple methods with the same name exist , But the required formal parameter list is inconsistent ( Method name must be the same , The formal parameter list must be different ( Number of formal parameters 、 Or type 、 Or in different order ), There is no requirement for return type )
Code example :m Method overloading
class Methods{
public int m(int n){
return n*n;
}
public int m(int n,int a){
return n*a;
}
public String m(String c){
return c;
}
}Two 、 Method rewriting ( Methods cover )
Method rewriting ( Cover ) The subclass has a method , And the name of a method of the parent class 、 Return type 、 Same parameter , Then we will say that the methods of this subclass cover the methods of the parent class .
Method rewriting needs to meet the following conditions :
(1) Parameters of subclass methods 、 The method name should be exactly the same as that of the parent class
(2) The return type of the subclass method is the same as that of the parent class , Or a subclass of the return type of the parent class
(3) A subclass method cannot reduce the access rights of a parent method
Code example :
Both parent and child classes have say Method is the rewriting of method (set and get Method is to operate on private attributes )
// Parent class person
public class Person {
private int age;
public Person( int age) {
this.age = age;
}
public String say(){
System.out.println("Person Self introduction. :");
return " age="+age;
}
// Subclass student
public class Student extends Person{
private String id;
public Student(int age, String id) {
super(age);
this.id = id;
}
public String say(){
System.out.println("Student Self introduction. :");
return super.say()+"id="+id;
}
}3、 ... and 、 Comparison between method overloading and method rewriting
| name | Range of occurrence | Method name | Return type | Parameter list | Modifier |
| heavy load | In this category | It has to be the same | No requirements | type 、 At least one difference in order or number | No requirements |
| rewrite | Parents and children | It has to be the same | The return type of the subclass method is the same as that of the parent class , Or a subclass of the return type of the parent class | identical | Subclass methods cannot narrow the access scope of parent methods |
边栏推荐
猜你喜欢
随机推荐
Can I view the history in the "stealth" mode of the secure browser?
What are the characteristics of low code tools? The two development tracks of low code that can be seen by discerning people!
Huawei released the top ten trends in 2025: 5g, robot, AI, etc
(C language) a brief introduction to define
方法重载与方法重写
拖放表格行
基于memcache的缓存机制的6个指令
Ren Zhengfei talked about the suppression of the United States again: to live is to win, and to defeat the United States
技术分享 | 服务端接口自动化测试, Requests 库的这些功能你了解吗?
Happens-Before原则深入解读
npm, npm中文文档, npm学习使用
Object. getOwnPropertyNames() VS Object.keys()
Selenium automated test interview questions family bucket
2022年简历石沉大海,别投了,软件测试岗位饱和了....
contenteditable 元素的placeholder
彻底搞通服务发现的原理和实现
Show load indicator when loading iframe
6、 Wechat applet release process
Kalibr calibration realsensed435i -- multi camera calibration
会用redis吗?那还不快来了解下redis protocol









