当前位置:网站首页>方法的重写
方法的重写
2022-08-04 18:08:00 【弱冠初心】
方法重写或方法的覆盖(overriding)
- 子类根据需求对从父类继承的方法进行重新编写
- 重写时,可以用super.方法的方式来保留父类的方法
- 构造方法不能被重写
父类:Person
子类: Pupil
public class Person {
String name;//父类属性
String age;//父类属性
//父类有参构造方法
public Person(String name, String age) {
super();
this.name = name;
this.age = age;
}
public String names(){
return name;
}
//打印信息,自我介绍
void personInfo(){
System.out.print("我叫"+this.name+",今年"+this.age);
}
public Person() {
super();
}
//重写toString()方法
@Override
public String toString() {
return "我叫:"+this.name+"今年:"+this.age;
}
//重写equals()方法
public boolean equals(Pupil pup) {
if(pup.name.equals(this.name )){
return true;
}else{
return false;
}
}
//子类继承父类
public class Pupil extends Person {
String pupilStudent;//子类特有的属性
//子类构造方法
public Pupil(String name, String age, String pupilStudent) {
super(name, age);
this.pupilStudent = pupilStudent;
}
//重写父类打印语句,自我介绍
@Override
void personInfo() {
super.personInfo();//调用父类方法
System.out.print(" ,我是小学生");
}
//mian方法,进行测试
public static void main(String[] args) {
Pupil pup = new Pupil("苏雨", "18岁", " ");
pup.personInfo();
System.out.println(pup);
System.out.println(pup.toString());
Pupil pup2 = new Pupil("苏雨", "18岁", " ");
System.out.println(pup.equals(pup2));
}
方法重写的原则:
重写与重载之间的区别 :从下面两张图理解

Object类被子类经常重写的方法
| 方法 | 说明 |
| toString() | 返回当前对象本身的有关信息,按字符串对象返回 |
| equals() | 比较两个对象是否是同一个对象,是则返回true |
| hashCode() | 返回该对象的哈希代码值 |
| getClass() | 获取当前对象所属的类信息,返回Class对象 |
边栏推荐
- Thrift IDL Sample File
- EasyCVR本地接入国标设备映射公网后,本地设备出现无法播放与级联的解决方法
- Interval greedy (interval merge)
- Understanding of margin collapse and coincidence
- linux下Mysql的简单操作
- 【STM32】STM32单片机总目录
- The Industrial Metaverse Brings Changes to Industry
- 基于层次分析法的“内卷”指数分析
- How does the intelligent video surveillance platform EasyCVR use the interface to export iframe addresses in batches?
- buuctf(探险1)
猜你喜欢

悦刻难回巅峰

SQL优化最全总结 - MySQL(2022最新版)

图解LeetCode——899. 有序队列(难度:困难)

网站设计师:Nicepage 4.15 Crack By Xacker

自己经常使用的三种调试:Pycharm、Vscode、pdb调试

斯坦福:未来的RGB LED可以贴在你的皮肤上

OpenInfra Days China 2022|SelectDB与你共享 Apache Doris 在互联网广告业务中的实践

基于层次分析法的“内卷”指数分析

About the two architectures of ETL (ETL architecture and ELT architecture)

八猴渲染器是什么?它能干什么?八猴软件的界面讲解
随机推荐
About the two architectures of ETL (ETL architecture and ELT architecture)
DOM Clobbering的原理及应用
[Web Automation Test] Quick Start with Playwright, 5 minutes to get started
How does the intelligent video surveillance platform EasyCVR use the interface to export iframe addresses in batches?
语音识别学习资源
如何让 JS 代码不可断点
clickhouse 上下线表
谁能解答?从mysql的binlog读取数据到kafka,但是数据类型有Insert,updata,
通俗易懂-二维数组只能省略行不能省略列-人话版本
解决错误:The package-lock.json file was created with an old version of npm
八猴渲染器是什么?它能干什么?八猴软件的界面讲解
mood swings
OpenInfra Days China 2022|SelectDB与你共享 Apache Doris 在互联网广告业务中的实践
linux下Mysql的简单操作
Documentary on Security Reinforcement of Network Range Monitoring System (1)—SSL/TLS Encrypted Transmission of Log Data
npm配置国内镜像(淘宝镜像)
2022年7月31日 暑假第三周总结
unity中实现ue眼球的渲染
DHCP&OSPF组合实验演示(Huawei路由交换设备配置)
2022 May 1 Mathematical Modeling Question C Explanation