当前位置:网站首页>“重载”与“重写”的区别
“重载”与“重写”的区别
2022-08-08 06:28:00 【记得爱蓝色】
首先我们要知道这两个词是完全不同的意思:
重载(Overload)是指:在同一个类中可以定义很多方法,但很多的方法实现的功能是相同的,只是参数不同(类型,顺序,个数),我们把这种相同方法名,只有参数不同称为方法重载。
例如:
public class Demo02 {
public static void main(String[] args) {
//方法的重载:在同一个类中,针对相同功能的一组同名方法的定义,方法的参数(类型、个数、顺序)
Demo02 dm = new Demo02();
dm.food("芝芝莓莓");
}
public void food() {
System.out.println("炒年糕");
}
public void food(String name) {
System.out.println("你想吃:" + name);
}
public void food(String name, double price) {
if(price > 0 ) {
System.out.println("食品是:" + name + "价格为:" + price);
}else {
System.out.println("请重新输入!");
}
}
}
值得注意的是:重载的返回值类型都是相同的。
了解到重载后,我们来具体看一下重写。
重写(Override)是指:在继承关系中,子类定义了一个与父类完全相同的方法被称为重写。
重写的意义在于子类在使用父类的方法时某些方法并没有达到自己想要的运行结果,对原方法进行重写。
我们来以toString()方法为例:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}在Object类中toString()方法返回的结果是类名[email protected] + 哈希值,但有时我们并不希望返回这个结果,于是对toString()方法进行重写:
public class Demo03 {
public static void main(String[] args) {
Object obj = new Object();
String str1 = obj.toString();
System.out.println(str1 );
Book book = new Book("悲惨世界","雨果");
String str2 = book.toString();
System.out.println(str2);
Book bookx = new Book("西游记", "吴承恩");
System.out.println(bookx); //自动调用对象的toString()方法
}
}
//图书类
class Book {
private String bookName;
private String author;
// 构造方法
public Book(String bookName, String author) {
this.bookName = bookName;
this.author = author;
}
//重写toString()
@Override
public String toString() {
String s = String.format("<<%s>>------%s", bookName,author);
return s;
}
进行重写以后,调用toString()方法返回的就是我们想要的:
<<悲惨世界>>------雨果:
<<西游记>>------吴承恩
边栏推荐
- golang 服务大量 CLOSE_WAIT 故障排查
- Code and ideas for implementing perpetual calendar in C language (detailed tutorial)
- 【Android安全】Binder解析
- How to get all child objects under an object in Unity
- Nine common interfaces for implementing sequence table in C language
- 关于在finally代码块中修改try代码块中基本数据类型返回值的问题
- Background Suppression Network for Weakly-supervised Temporal Action Localization
- 【Android安全】ActivityManager.isUserAMonkey API
- re模块,初识爬虫,openpyxl模块
- Day42-43
猜你喜欢
随机推荐
性能测试------LoadRunner
Unity_条形图(柱状图)+ UI动画
[Unity] C#使用委托事件与字典实现unity消息中心(观察者模式)
【Android安全】Oneplus 5T root 刷机 救砖
背包问题小结
【Android安全】AOSP源码在线阅读
MySQL的DDL和DML
Unity_雷达图(属性图)+ UI动画
【Excel】csv文件修改分隔符
Code and ideas for implementing perpetual calendar in C language (detailed tutorial)
Unity—ParticleSystem (particle system) and Animator (animation state machine) batch manager
NVIDIA CUDA Highly Parallel Processor Programming (VII): Parallel Mode: Prefix and
List、Set、Map、Queue、Deque、Stack遍历方式小结
By using the fgets () the number of rows in the statistics file and use the fgets ()/fputs () copy files
【Android安全】priv-app 系统应用权限
超大Excel文件读写 :使用SXSSFWorkbook和EasyExcel方式对比
[Unity] 自定义日志系统 解决Unity Log的痛点
网络安全笔记第二天day2(等级保护)
Solved the problem that when VRTK transmission under Unity HDRP, the screen fades in and out, and the visual occlusion cannot be displayed correctly when passing through the wall
自定义类加载器









