当前位置:网站首页>Day019 方法重写与相关类的介绍
Day019 方法重写与相关类的介绍
2022-08-05 04:15:00 【陌 年】
一、方法重写
1、方法重写的概念
(1)子类根据需求对从父类继承的方法进行重新编写
(2)重写时,可以用super.方法的方式来保留父类的方法
(3)构造方法不能被重写
2、方法重写规则
(1)方法名相同
(2)参数列表相同
(3)返回值类型相同或者是其子类
(4)访问权限不能严于父类
(5)父类的静态方法不能被子类覆盖为非静态方法,父类的非静态方法不能被子类覆盖为静态方法
(6)子类可以定义与父类同名的静态方法,以便在子类中隐藏父类的静态方法(注:静态方法中无法使用super)
(7)父类的私有方法不能被子类覆盖
(8)不能抛出比父类方法更多的异常
2、方法重写与方法重载的区别

二、Object类
Object类:是所有类的父类
Object类被子类经常重写的方法

1、Object类的equals()方法
(1)比较来俩个对象是否是同一个对象,是则返回true
(2)操作符
①简单数据类型,直接比较值。如1==2
②引用类型,比较两者是否为同一对象
注意:
(1)Object类的equals()方法与==没区别
(2)当有特殊需求,如认为属性相同即为同一对象时,需要重写equals()
(3)Java.lang.String重写了equals()方法,把equals()方法的判断变为了判断其值
2、练习

package cn.bdqn.demo04;
import java.util.Objects;
public class Student {
private String name;
private int idCard;
private int age;
public Student() {
}
public Student(String name, int idCard, int age) {
this.name = name;
this.idCard = idCard;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIdCard() {
return idCard;
}
public void setIdCard(int idCard) {
this.idCard = idCard;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return idCard == student.idCard && age == student.age && Objects.equals(name, student.name);
}
}
package cn.bdqn.demo04;
import java.sql.SQLOutput;
public class Test {
public static void main(String[] args) {
Student stu1=new Student("张三",1234,22);
Student stu2=new Student("xx",1234,22);
System.out.println(stu1.equals(stu2));
}
}

边栏推荐
- How to solve complex distribution and ledger problems?
- [CISCN2019 华东南赛区]Web11
- UE4 在游戏运行时更改变量 (通过鼠标滑轮来更改第一人称角色的最大行走速度)
- markdown如何换行——md文件
- UE4 opens doors with overlapping events
- Summary of common methods of arrays
- How to solve the three major problems of bank data collection, data supplementary recording and index management?
- Machine Learning Overview
- Bytebuffer put flip compact clear method demonstration
- UE4 第一人称角色模板 添加生命值和调试伤害
猜你喜欢

There are several common event handling methods in Swing?How to listen for events?

C++ core programming

从企业的视角来看,数据中台到底意味着什么?

4T硬盘剩余很多提示“No space left on device“磁盘空间不足

The log causes these pits in the thread block, you have to guard against

小程序_动态设置tabBar主题皮肤

开发属于自己的node包

How to solve complex distribution and ledger problems?

A 35-year-old software testing engineer with a monthly salary of less than 2W, resigns and is afraid of not finding a job, what should he do?

The most effective seven performance testing techniques of software testing techniques
随机推荐
How to discover a valuable GameFi?
C+ +核心编程
UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法
Spark基础【介绍、入门WordCount案例】
There are a lot of 4T hard drives remaining, prompting "No space left on device" insufficient disk space
GC Gaode coordinate and Baidu coordinate conversion
What is ASEMI photovoltaic diode, the role of photovoltaic diode
不看后悔,appium自动化环境完美搭建
机器学习概述
Redis key basic commands
说说数据治理中常见的20个问题
银行数据采集,数据补录与指标管理3大问题如何解决?
[MRCTF2020]PYWebsite
从企业的视角来看,数据中台到底意味着什么?
mutillidae下载及安装
Mysql的undo log详解
10 years of testing experience, worthless in the face of the biological age of 35
【informix】解决启动报错大全,以及解决办法
Hard power or soft power, which is more important to testers?
UE4 通过互动(键盘按键)开门