当前位置:网站首页>Day019 Method overriding and introduction of related classes
Day019 Method overriding and introduction of related classes
2022-08-05 04:21:00 【Strange year】
One, method rewriting
1. The concept of method overriding
(1) The subclass rewrites the method inherited from the parent class according to the requirements
(2) When rewriting, you can use the super. method to retain the method of the parent class
(3) The constructor cannot be overridden
2. Method rewrite rules
(1) The method name is the same
(2) The parameter list is the same
(3) The return value type is the same or its subclass
(4) The access authority cannot be stricter than the parent class
(5) The static method of the parent class cannot be overridden as a non-static method by the child class, and the non-static method of the parent class cannot be overridden as a static method by the child class
(6) A subclass can define a static method with the same name as the parent class, so as to hide the static method of the parent class in the subclass (Note: super cannot be used in static methods)
(7) The private method of the parent class cannot be overridden by the child class
(8) Cannot throw more exceptions than the parent class method
2. The difference between method overriding and method overloading
Second, Object class
Object class: is the parent class of all classes
Methods of the Object class that are often overridden by subclasses
1. equals() method of Object class
(1) Compare whether the two objects are the same object, if yes, return true
(2) operator
1 Simple data type, compare the value directly.Such as 1==2
②Reference type, compare whether the two are the same object
Note:
(1) The equals() method of the Object class is no different from ==
(2) When there are special requirements, such as when the same attributes are considered to be the same object, equals() needs to be rewritten
(3) Java.lang.String rewrites the equals() method, changing the judgment of the equals() method into judging its value
2. Exercise
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;}@Overridepublic 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("Zhang San",1234,22);Student stu2=new Student("xx",1234,22);System.out.println(stu1.equals(stu2));}}
边栏推荐
- Four-digit display header design
- Cron(Crontab)--使用/教程/实例
- Why did you start preparing for the soft exam just after the PMP exam?
- Android interview question - how to write with his hands a non-blocking thread safe queue ConcurrentLinkedQueue?
- 2022软件测试工程师最全面试题
- 多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层
- 七夕节赚徽章拉
- Develop your own node package
- Application status of digital twin technology in power system
- Summary of common methods of arrays
猜你喜欢
UE4 第一人称角色模板 添加蹲伏功能
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?
Analyses the mainstream across technology solutions
Machine Learning Overview
【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
不看后悔,appium自动化环境完美搭建
从企业的视角来看,数据中台到底意味着什么?
Haproxy搭建Web群集
dedecms后台生成提示读取频道信息失败的解决方法
flink读取mongodb数据源
随机推荐
多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层
UE4 为子弹蓝图添加声音和粒子效果
DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏
The first performance test practice, there are "100 million" a little nervous
36-Jenkins-Job Migration
【测量学】速成汇总——摘录高数帮
mutillidae下载及安装
Analyses the mainstream across technology solutions
The production method of the powered small sailboat is simple, the production method of the electric small sailboat
MySql index learning and use; (I think it is detailed enough)
iMedicalLIS监听程序(2)
关于#SQL#的迭代、父子结构查询问题,如何解决?
[8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
浅析主流跨端技术方案
UE4 第一人称角色模板 添加生命值和调试伤害
Mysql's redo log detailed explanation
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
日志导致线程Block的这些坑,你不得不防
App rapid development and construction experience: the importance of small programs + custom plug-ins
四位数显表头设计