当前位置:网站首页>Overloading and overriding
Overloading and overriding
2022-06-26 05:43:00 【Wang Xiaoya】
Method overloading
It is simply a function or method Have the same name , however The parameter list is not the same The circumstances of , In this way Different parameters with the same name Between functions or methods of , Call each other overloaded function perhaps Overloading methods
Method overloading refers to the relationship between multiple methods defined in the same class , Multiple methods that satisfy the following conditions constitute overloading each other .
1) Multiple methods in the same class
2) Multiple methods have the same method name
3) Multiple methods have different parameters , Different in type or quantity
Which of the following programs is related to show() Method overloading ()?**A、C、D**
class Demo{
void show(int a,int b,float c){}
}
A.void show(int a,float c,int b){} //√ Parameter order is different
B.void show(int a,int b,float c){} //× Exactly the same
C.int show(int a,float c,int b){return a;} //√ The same name , Parameter order is different
D.int show(int a,float c){return a //√ Different parameters
Method overload features :
* Overloading only corresponds to the definition of the method , It has nothing to do with the method call , The call mode refers to the standard format
* Overloading only identifies the names and parameters of methods in the same class , Independent of the return value , In other words, the return value cannot be used to determine whether two methods constitute overloads
Method rewriting
* 1、 Method rewriting concept
* As like as two peas, the subclass has the same method declaration as the parent class ( The method name is the same , The parameter list must be the same )
* 2、 Application scenarios of method rewriting
* When a child class needs the function of a parent class , When the function subject subclass has its own content , You can override methods in the parent class , such , That is, following the function of the parent class , It also defines the specific content of the subclass
Specific examples : Cats and animals
Cats are animals , Inherited the animal class , Have the same method “eat”, And rewritten eat, The final output is also rewritten “ Cats eat fish ”
Can combine http://t.csdn.cn/fqWv6 understand
package com.object_02;
public class Animal9 {
public int age = 40;
public void eat() {
System.out.println(" Animals eat ");
}
}
package com.object_02;
import com.object_02.Animal9;
public class Cat9 extends Animal9 {
public int age = 20;
public int weight = 10;
@Override
public void eat() {
System.out.println(" Cats eat fish ");
}
public void playGame() {
System.out.println(" Cat hide and seek ");
}
}
package com.object_02;
public class AnimalDemo9 {
public static void main(String[] args) {
// There are parent class references to child class objects
Animal9 a = new Cat9();
System.out.println(a.age);
// System.out.println(a.weight);
// Because the member variable Compile and run to see the parent class , There is no , therefore weight Report errors
// Run out age Is also a parent class 40
a.eat();
// a.playGame();
// Because the member method compilation looks at the parent class , Parent class Animal There are only eat Method , therefore playGame Method error reporting
// Because the subclass can be seen during operation , therefore eat The result of method running is the subclass overridden " Cats eat fish "
}
}
3、Override annotation
* To detect the current method , Whether it's an overriding method , Play a 【 check 】 The role of 2.7 Method rewriting considerations ( master )
* Method rewriting considerations
1. Private methods cannot be overridden ( Private members of a parent class cannot be inherited from a child class )
2. Subclass method access cannot be lower (public > Default > private )
The difference between overloading and overriding is as follows ( expand )
1. Different definitions --- Overloading is to define the same method name , Different parameters ; Overriding is a method that a subclass overrides a parent class .
2. The range is different --- Overloading is in a class , Overriding is between a subclass and its parent .
3. Polymorphic difference --- Overloading is compile time polymorphism , Run time polymorphism is overridden .
4. Back to different --- Overload does not require a return type , Rewriting requires a return type , There are compatible return types .
5. Different parameters --- Number of overloaded parameters 、 Parameter type 、 The order of parameters can be different , The parameters of overriding parent-child methods must be the same .
6. Embellishment is different --- Overloading has no special requirements for access decoration , The restriction of overriding the access modifier must be greater than that of the overridden method .
边栏推荐
猜你喜欢
REUSE_ ALV_ GRID_ Display event implementation (data_changed)
AutowiredAnnotationBeanPostProcessor什么时候被实例化的?
cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching
怎么把平板作为电脑的第二扩展屏幕
小小面试题之GET和POST的区别
[upsampling method opencv interpolation]
10 set
【 langage c】 stockage des données d'analyse approfondie en mémoire
pytorch(环境、tensorboard、transforms、torchvision、dataloader)
Introduction to GUI programming to game practice (I)
随机推荐
uni-app吸顶固定样式
Pytorch (network model training)
The use of loops in SQL syntax
【PHP】PHP二维数组按照多个字段进行排序
Data storage: the difference between MySQL InnoDB and MyISAM
The difference between get and post in small interview questions
421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)
组合模式、透明方式和安全方式
原型模式,咩咩乱叫
SQL query time period content
操作符的优先级、结合性、是否控制求值顺序【详解】
[red team] what preparations should be made to join the red team?
Redis discovery bloom filter
Sofa weekly | open source person - Yu Yu, QA this week, contributor this week
慢慢学JVM之缓存行和伪共享
基于SDN的DDoS攻击缓解
2021年OWASP-TOP10
SOFA Weekly | 开源人—于雨、本周 QA、本周 Contributor
12 multithreading
data = self._ data_ queue. get(timeout=timeout)