当前位置:网站首页>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
商业智能BI业务分析思维:现金流量风控分析(一)营运资金风险
write the story about us
No regrets, the appium automation environment is perfectly built
动力小帆船制作方法简单,电动小帆船制作方法
AUTOCAD - dimension association
银行数据采集,数据补录与指标管理3大问题如何解决?
UE4 第一人称角色模板 添加冲刺(加速)功能
Detailed explanation of Mysql's undo log
In the hot summer, teach you to use Xiaomi smart home accessories + Raspberry Pi 4 to connect to Apple HomeKit
随机推荐
[MRCTF2020] PYWebsite
程序开发的一些常规套路(一)
The most comprehensive exam questions for software testing engineers in 2022
Event parse tree Drain3 usage and explanation
Cron(Crontab)--use/tutorial/example
The test salary is so high?20K just graduated
write the story about us
The log causes these pits in the thread block, you have to guard against
bytebuffer 使用demo
NPDP证书含金量高吗?跟PMP相比?
UE4 为子弹蓝图添加声音和粒子效果
What is the function of industrial-grade remote wireless transmission device?
多御安全浏览器新版下载 | 功能优秀性能出众
flink读取mongodb数据源
Qixi Festival code confession
DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏
pyqt5 + socket 实现客户端A经socket服务器中转后主动向客户端B发送文件
App rapid development and construction experience: the importance of small programs + custom plug-ins
[8.3] Code Source - [meow ~ meow ~ meow~] [tree] [and]
dedecms织梦tag标签不支持大写字母修复