当前位置:网站首页>overlord(重载)与override(重写)的区别
overlord(重载)与override(重写)的区别
2022-08-07 05:12:00 【一只码农菜汪】
前言
overlord(重载)与override(重写)有什么区别?
提示:以下是本篇文章正文内容,下面案例可供参考
一、overlord(重载)
在一个类中,我们可以定义多个方法。如果有一系列方法,它们的方法名相同,功能类似只有参数不同(参数类型、参数顺序、参数个数),这就是方法的重载。
注意:方法的重载只能在同一个类中
代码如下(示例):
class Demo{
public void student(int age) {
System.out.println("年龄:"+age);
}
//参数类型不同
public void student(String name) {
System.out.println("姓名:"+name);
}
//参数个数不同
public void student(String name,int age) {
System.out.println("姓名:"+name+",年龄:"+age);
}
//参数顺序不同
public void student(int age,String name) {
System.out.println("姓名:"+name+",年龄:"+age);
}
}
二、override(重写)
在继承关系中,如果子类定义了一个与父类中方法签名(返回值类型,方法名,参数类型)完全相同的方法,被称为重写。
代码如下(示例):
//重写
class Person{
public void say(){
System.out.println("Hello");
}
}
class Student extends Person{
//重写父类say()方法
@Override
public void say() {
System.out.println("Hello,teacher");
}
}
边栏推荐
猜你喜欢

Automated operation and maintenance tools - ansible overview and deployment

动态规划之背包问题

NSSCTF Round#4 Web WP

Sigrity PowerDC仿真

One Pass 1225 - Treasure Island

远程连接 redis 时,报错 (error) DENIED Redis is running in protected mode because protected mode is enabled

cftshow 七夕杯 web部分WP

IDEA 2022.2 released

网线Cable

Network cable
随机推荐
【SemiDrive源码分析】【MailBox核间通信】48 - 将RPMSG IPCC RPC 单次传输数据大小修改为 512字节(代码实战部分)
RT_thread的IIC设备学习笔记
2021-01-05
Detailed explanation of C51 basic functions, interrupt functions and library functions
Linear Algebra Study Notes 4-6: Null Space, Column Space, Row Space, Left Null Space, Elementary Row Transformation, Test Questions of Matrix
洛谷P1202 黑色星期五Friday the Thirteenth
一周活动速递|深入浅出第8期;Meetup成都站报名进行中
Sigrity PowerDC仿真
洛谷P3521 ROT-Tree Rotations
无聊的冷知识4
pymysql格式化输入的一些问题
Automated operation and maintenance tools - ansible overview and deployment
页面底部出现横向滚动条解决方法
后勤仓库物资领用发放小程序开发制作功能介绍
《国际学术论文写作与发表》参考答案
TypeScript入门知识总结
软删除源码分析
全网最全超详细.htaccess语法讲解
远程连接 redis 时,报错 (error) DENIED Redis is running in protected mode because protected mode is enabled
Linear Algebra Study Notes 4-5: Solving Systems of Linear Equations