当前位置:网站首页>方法的重写
方法的重写
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对象 |
边栏推荐
- Flask framework implementations registered encryption, a Flask enterprise class learning 】 【
- Introduction of three temperature measurement methods for PT100 platinum thermal resistance
- Nintendo won't launch any new hardware until March 2023, report says
- darknet源码阅读笔记-02-list.h和lish.c
- 阿里云国际版使用ROS搭建WordPress教程
- 企业调查相关性分析案例
- A group of friends asked for help, but the needs that were not solved in a week were solved in 3 minutes?
- July 31, 2022 Summary of the third week of summer vacation
- 关于ETL的两种架构(ETL架构和ELT架构)
- 通配符SSL证书不支持多域名吗?
猜你喜欢

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

巴比特 | 元宇宙每日必读:微博动漫将招募全球各类虚拟偶像并为其提供扶持...

Flask框架实现注册加密功能详解【Flask企业课学习】

2019年海淀区青少年程序设计挑战活动小学组复赛试题详细答案

网页端IM即时通讯开发:短轮询、长轮询、SSE、WebSocket

【无标题】

【软件工程之美 - 专栏笔记】37 | 遇到线上故障,你和高手的差距在哪里?

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

开发那些事儿:如何通过EasyCVR平台获取监控现场的人流量统计数据?

Develop those things: How to obtain the traffic statistics of the monitoring site through the EasyCVR platform?
随机推荐
FE01_OneHot-Scala Application
企业调查相关性分析案例
【STM32】入门(五):串口TTL、RS232、RS485
margin 塌陷和重合的理解
数仓相关,总结
Error when using sourcemap for reporting an error: Can‘t resolve original location of error.
Introduction of three temperature measurement methods for PT100 platinum thermal resistance
OpenInfra Days China 2022 | SelectDB to share with you the Apache Doris in Internet advertising business practices
Web端即时通讯技术:WebSocket、socket.io、SSE
Babbitt | Metaverse daily must-read: Weibo animation will recruit all kinds of virtual idols around the world and provide support for them...
LeetCode 899. Ordered Queues
Flink / Scala - 使用 RedisSink 存储数据
不论你是大众,科班和非科班,我这边整理很久,总结出的学习路线,还不快卷起来
2022年7月31日 暑假第三周总结
The prefix and discretization
DHCP&OSPF组合实验演示(Huawei路由交换设备配置)
静态iP与权限更改[通俗易懂]
谷歌开源芯片 180 纳米制造工艺
#yyds干货盘点# 面试必刷TOP101:链表相加(二)
DMPE-PEG-Mal,二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺简述