当前位置:网站首页>方法的重写
方法的重写
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对象 |
边栏推荐
- SQL优化最全总结 - MySQL(2022最新版)
- How does EasyCVR call the double-speed playback of device recording through the interface?
- Flask framework implementations registered encryption, a Flask enterprise class learning 】 【
- 谁能解答?从mysql的binlog读取数据到kafka,但是数据类型有Insert,updata,
- ATF中断处理的设计模型
- darknet源码阅读笔记-02-list.h和lish.c
- Create Sentinel high-availability cluster current limiting middleware from -99
- 离线同步odps到mysql 中文乱码是因为?mysql已是utf8mb4
- 【STM32】入门(五):串口TTL、RS232、RS485
- 离散化求前缀和
猜你喜欢
JS兼容问题总结
基于 eBPF 的 Kubernetes 可观测实践
从-99打造Sentinel高可用集群限流中间件
Cholesterol-PEG-Maleimide,CLS-PEG-MAL,胆固醇-聚乙二醇-马来酰亚胺一种修饰性PEG
Introduction of three temperature measurement methods for PT100 platinum thermal resistance
The Industrial Metaverse Brings Changes to Industry
CAN光纤转换器CAN光端机解决消防火灾报警
【软件工程之美 - 专栏笔记】37 | 遇到线上故障,你和高手的差距在哪里?
Matlab画图1
Investigation and Research Based on the Involution Behavior of College Students
随机推荐
flink-cdc支持并行读取一张mysql表的binlog不?
#yyds干货盘点# 面试必刷TOP101:链表相加(二)
How to recruit programmers
通俗易懂-二维数组只能省略行不能省略列-人话版本
基于激励的需求响应计划下弹性微电网的短期可靠性和经济性评估(Matlab代码实现)
企业调查相关性分析案例
Google Earth Engine APP - one-click online viewing of global images from 1984 to this year and loading an image analysis at the same time
Cholesterol-PEG-DBCO,CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔科研试剂
不论你是大众,科班和非科班,我这边整理很久,总结出的学习路线,还不快卷起来
Create Sentinel high-availability cluster current limiting middleware from -99
Understanding of margin collapse and coincidence
情绪的波动起伏
mood swings
2019 Haidian District Youth Programming Challenge Activity Elementary Group Rematch Test Questions Detailed Answers
静态iP与权限更改[通俗易懂]
OpenInfra Days China 2022 | SelectDB to share with you the Apache Doris in Internet advertising business practices
LeetCode 899. 有序队列
Global electronics demand slows: Samsung's Vietnam plant significantly reduces capacity
Regardless of whether you are a public, professional or non-major class, I have been sorting out the learning route for a long time here, and the learning route I have summarized is not yet rolled up
Cholesterol-PEG-Maleimide,CLS-PEG-MAL,胆固醇-聚乙二醇-马来酰亚胺一种修饰性PEG