当前位置:网站首页>重载和重写
重载和重写
2022-08-10 19:12:00 【TPH-BETTER】
转自https://blog.csdn.net/qq_44257807/article/details/115601953
一、重载和重写的概念:
重载的概念 :
在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数 类型不同即可。
重写的概念:
在子类中可以根据需要对从父类中继承来的方法进行改造,也称 为方法的重置、覆盖。在程序执行时,子类的方法将覆盖父类的方法。
二、重载和重写的特点:
重载的特点:
与返回值类型无关,只看参数列表,且参数列表必须不同。(参数个数或参数类 型)。调用时,根据方法参数列表的不同来区别。
重写的特点:
1.子类重写的方法必须和父类被重写的方法具有相同的方法名称、参数列表 。
2.子类重写的方法的返回值类型不能大于父类被重写的方法的返回值类型 (返回类型为类)。
3.子类重写的方法使用的访问权限不能小于父类被重写的方法的访问权限 。
4.子类不能重写父类中声明为private权限的方法 。
5.子类方法抛出的异常不能大于父类被重写方法的异常 (父类异常的子类)。
重载示例
//返回两个整数的和
int add(int x,int y){
return x+y;}
//返回三个整数的和
int add(int x,int y,int z){
return x+y+z;}
//返回两个小数的和
double add(double x,double y){
return x+y;}
重写示例:
public class Person{
public String name;
public int age;
public String getInfo(){
return “Name:” + name + “\n” + “age:” + age;
}
}
public class Student extends Person{
public String school;
public String getInfo(){
return "Name: "+ name + "\nage: "+ age + "\nschool: "+ school;
}
public static void main(String[] args){
Person p1=new Person();
//调用Person的getInfo方法
p1.getInfo();
Student s1 = new Student();
//调用Student的getInfo方法
s1.getInfo();
}
}
三、简述Java中重写和重载的区别:
重写,overriding,是指在继承的情况下,子类中定义了与父类中具有相同型构的新方法,也就是子类重写了父类的方法。
重载,overloading,是在同一个类中,定义了一个以上具有相同名称,但是有着不同型构的方法,在同一个类中是不允许定义多于一个具有相同型构的方法的。
构造器也是可以重载的,实际上,构造器就是一个方法,构造器的名称就是方法的名称。
边栏推荐
- (12) findContours function hierarchy explanation
- whois information collection & corporate filing information
- QoS服务质量六路由器拥塞管理
- redis如何查看key的有效期
- 子域名收集&Google搜索引擎语法
- [Teach you how to make a small game] Write a function with only a few lines of native JS to play sound effects, play BGM, and switch BGM
- 主动信息收集
- 怎么完全卸载赛门铁克_Symantec卸载方法,赛门铁克卸载「建议收藏」
- YOLOv3 SPP源码分析
- 【毕业设计】基于STM32的天气预报盒子 - 嵌入式 单片机 物联网
猜你喜欢
Common ports and services
测试/开发程序员值这么多钱么?“我“不会愿赌服输......
【无标题】基于Huffman和LZ77的GZIP压缩
子域名收集&Google搜索引擎语法
【C#】WCF和TCP消息通信练习,实现群聊功能
[email protected])纳米酶"/>
血红素-金纳米颗粒(Heme-AuNP)复合纳米酶|金纳米颗粒核多孔空心碳纳米球壳([email protected])纳米酶
【知识分享】在音视频开发领域中SEI到底是个啥?
Pt/CeO2 monatomic nanoparticles enzyme | H - rGO - Pt @ Pd NPs enzyme | carbon nanotube load platinum nanoparticles peptide modified nano enzyme | leukemia antagonism FeOPtPEG composite nano enzyme
YOLOv3 SPP source analysis
QoS Quality of Service Seven Switch Congestion Management
随机推荐
【毕业设计】基于Stm32的智能疫情防控门禁系统 - 单片机 嵌入式 物联网
2022杭电多校七 Black Magic (签到)
QoS Quality of Service Seven Switch Congestion Management
巧用RoaringBitMap处理海量数据内存diff问题
杭电多校七 1003-Counting Stickmen(组合数学)
3D Game Modeling Learning Route
机器学习|模型评估——AUC
zip文件协议解析
Today's bug, click on the bug that the Windows dynamic wallpaper disappears in the win10 taskbar, and no solution has been found yet.
What is the upstream bandwidth and downstream bandwidth of the server?
Heme - gold nanoparticles (Heme - AuNP) composite nanometer enzyme | gold nanoparticles nuclear porous hollow carbon nanometer spherical shell (Au @ HCNs) nano enzyme
cordova 安装错误 Command failed: powershell 解决方法
QoS服务质量七交换机拥塞管理
Keras深度学习实战(17)——使用U-Net架构进行图像分割
铁蛋白-AHLL纳米颗粒|人表皮生长因子-铁蛋白重链亚基纳米粒子(EGF-5Cys-FTH1)|铁蛋白颗粒包载氯霉素Chloramphenicol-Ferritin
[CNN] Brush SOTA's trick
【无标题】基于Huffman和LZ77的GZIP压缩
About npm/cnpm/npx/pnpm and yarn
电脑开不了机是什么原因?
Colocate Join :ClickHouse的一种高性能分布式join查询模型