当前位置:网站首页>Overloading and rewriting
Overloading and rewriting
2022-07-22 11:43:00 【lilshork】
heavy load
Overloading is in a class , The method name is the same , And the parameters are different . The return type can be the same or different .
In a program , If you want to use the same method , And when using methods , Different data types are used , Or the number of data 、 Different order , Method overloading can be used .
rewrite
Rewriting requires inheritance , Same method name , Parameters are the same , Overrides the method of the parent class .
Method rewriting needs to meet :
1. Methods in the parent class and methods in the subclass
2. Method names must be consistent
3. Method parameters must be consistent
4. Access modifiers of subclass methods cannot narrow access permissions
5. The return value type should be consistent
There is an animal , There is a way of sleeping in animals , Cats and dogs also have ways to sleep
public class Animal {
public void sleep(){
System.out.println(" Animals sleep ");
}
}
Create a dog class , It also has a way to sleep
public class Dog extends Animal{
@Override
public void sleep() {
System.out.println(" Dogs sleep ");
}
}
Create a cat class , It also has a way to sleep
public class Cat extends Animal{
@Override
public void sleep() {
System.out.println(" Cats sleep ");
}
}
Create a Test Class test
public class Test03 {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sleep();
Cat cat = new Cat();
cat.sleep();
}
}
result :
summary :
The overridden method parameter list must be the same , The overloaded method parameter list must be different , The return value type of the overridden method can only be the parent type or the subclass of the parent type , Overloaded methods do not require the return value type .
边栏推荐
- I wipe the "hidden rules" in the interview of those testers. Don't step on the pit
- 尾递归调用过程梳理
- Embedded sharing collection 18
- 【uniCloud】云对象的应用与提升
- MySQL插入数据insert ignore和replace into
- Recommended reading: how can testers get familiar with new businesses quickly?
- Meta: predict the potential impact of the metauniverse from the development and innovation of mobile technology
- Findcontours and drawcontours & image contours (I) & cv2 boundingRect and cv2. rectangle
- 深入解析 pycocotools 的安装和运行报错 && 安装 mmcv-full and mmpycocotools
- Leetcode skimming -- drip record 020
猜你喜欢
Redis的主从复制
基于SSM+MySQL+Bootstrap+JQuery的在线购物商城电子商务系统
MySQL插入数据insert ignore和replace into
Redis分布式锁
即看即用 && 比较操作(Comparison Ops) && Pytorch官方文档总结 && 笔记 (七)
Opencv & crop video into image sets with a specified frame rate
论文分享目标检测、图像分割、监督学习等
PHP生成器的使用yield性能优化
grafana 监控 node
In depth analysis of pycocotools' installation and operation error & installation of mmcv full and mmpycocotools
随机推荐
Ready to use & serialization and parallelism & pytoch official document summary & Notes (IV)
使用 Opencv and OS or pathlib.Path 获取路径名称和图像名称,并保存图像到指定路径
Use of PHP generator yield performance optimization
Syntax comparison between ES6 class and Es5
Guess numbers + random numbers
(PC+WAP)织梦模板情感资讯类网站
LeetCode·987.二叉树的垂直遍历·桶排序·模拟
MySQL insert ignore and replace into
UE5 官方案例LyraStarter 全特性详解 5.LyraPlayerStart
PHP利用Redis 事务实现商品秒杀
(pc+wap) dream weaving template emotional information website
[brother hero July training] day 21: the smallest number in infinite concentration
Is it safe for flush to open Huatai Securities account?
openoffice启动和自动启动设置
滴滴被罚80.26亿元,马斯克携芭比娃娃造玩具,DALL-E邀请百万人加入测试,今日更多新鲜事在此
Routing policy-
移动端Web App 的屏幕适配方法(总结)
即看即用 && 数学操作 ( Math operations) && Pytorch官方文档总结 && 笔记 (五)
[unicloud] application and improvement of cloud objects
深度剖析问题:Could not run ‘torchvision::nms‘ with arguments from the ‘CUDA‘ backend.