当前位置:网站首页>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 .
边栏推荐
- 适配器模式
- 组合模式、透明方式和安全方式
- cartographer_ backend_ constraint
- Consul服务注册与发现
- Leetcode513.找出树的左下角的值
- [MySQL] MySQL million level data paging query method and its optimization
- Data storage: the difference between MySQL InnoDB and MyISAM
- [activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022
- data = self._ data_ queue. get(timeout=timeout)
- Cyclic displacement
猜你喜欢

Using Jenkins to perform testng+selenium+jsup automated tests and generate extendreport test reports

Command line interface of alluxio

工厂方法模式、抽象工厂模式

cartographer_ optimization_ problem_ 2d

Redis usage and memory optimization

The difference between get and post in small interview questions

Introduction to GUI programming to game practice (I)

Gram 矩阵

cartographer_ pose_ graph_ 2d
Posting - don't get lost in the ocean of Technology
随机推荐
Customize WebService as a proxy to solve the problem of Silverlight calling WebService across domains
skimage. morphology. medial_ axis
Summary of the 10th provincial Blue Bridge Cup
Daily production training report (17)
[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022
Pytorch中自己所定义(修改)的模型加载所需部分预训练模型参数并冻结
SDN based DDoS attack mitigation
pytorch(网络模型)
ZigBee learning in simple terms Lecture 1
Data storage: the difference between MySQL InnoDB and MyISAM
421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)
A love that never leaves
Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
REUSE_ALV_GRID_DISPLAY 事件实现(DATA_CHANGED)
基于SDN的DDoS攻击缓解
uniCloud云开发获取小程序用户openid
Owasp-top10 in 2021
The model defined (modified) in pytoch loads some required pre training model parameters and freezes them
【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...
生命原来如此脆弱
