当前位置:网站首页>方法的重写
方法的重写
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对象 |
边栏推荐
- C#爬虫之通过Selenium获取浏览器请求响应结果
- How does EasyCVR call the double-speed playback of device recording through the interface?
- 【web自动化测试】Playwright快速入门,5分钟上手
- A group of friends asked for help, but the needs that were not solved in a week were solved in 3 minutes?
- How to recruit programmers
- 基于层次分析法的“内卷”指数分析
- Thrift安装配置
- 2022年五一数学建模C题讲解
- 2022 May 1 Mathematical Modeling Question C Explanation
- 敏捷开发项目管理的一些心得
猜你喜欢

CAN光纤转换器CAN光端机解决消防火灾报警

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

力扣学习---0804

"No title"

企业即时通讯软件有哪些功能?对企业有什么帮助?

#yyds干货盘点# 面试必刷TOP101:链表相加(二)

Alibaba Cloud International Edition uses ROS to build WordPress tutorial

解决错误:The package-lock.json file was created with an old version of npm

Hezhou Cat1 4G module Air724UG is configured with RNDIS network card or PPP dial-up, and the development board is connected to the Internet through the RNDIS network card (taking the RV1126/1109 devel

2022年五一数学建模C题讲解
随机推荐
通配符SSL证书不支持多域名吗?
darknet source code reading notes-02-list.h and lish.c
悦刻难回巅峰
区间贪心(区间合并)
【web自动化测试】playwright安装失败怎么办
2022年五一数学建模C题讲解
Babbitt | Metaverse daily must-read: Weibo animation will recruit all kinds of virtual idols around the world and provide support for them...
容器化 | 在 NFS 备份恢复 RadonDB MySQL 集群数据
2018读书记
Documentary on Security Reinforcement of Network Range Monitoring System (1)—SSL/TLS Encrypted Transmission of Log Data
(ECCV-2022)GaitEdge:超越普通的端到端步态识别,提高实用性
About the two architectures of ETL (ETL architecture and ELT architecture)
如何模拟后台API调用场景,很细!
clickhouse 上下线表
从-99打造Sentinel高可用集群限流中间件
图解LeetCode——899. 有序队列(难度:困难)
斯坦福:未来的RGB LED可以贴在你的皮肤上
网站设计师:Nicepage 4.15 Crack By Xacker
July 31, 2022 Summary of the third week of summer vacation
哈夫曼树(暑假每日一题 15)