当前位置:网站首页>Access Characteristics of Constructor under Inheritance Relationship
Access Characteristics of Constructor under Inheritance Relationship
2022-08-09 17:48:00 【acccco】
I. What is inheritance?
The so-called inheritance, simply put, is that the subclass inherits the parent class.When multiple classes have common attributes or method behaviors, these common points can be put forward and put into a single class, which is called the parent class, and the rest of the classes are called subclasses.Many subclasses only need to inherit the methods of the parent class.Inheritance is one of the most significant features of object-oriented, that is, a new class is derived from an existing class. The new class can absorb the data attributes and behaviors of the existing class, and can extend new capabilities.
Second, the benefits of inheritance
Common extraction (extract the same attributes or methods) to improve code reusability.
Three. Characteristics of inheritance
1. Single inheritance, a subclass can only have one direct parent class;
2. A parent class can have multiple subclasses;
3.java allows multi-level inheritance
Four. Access characteristics of constructors under inheritance relationship
1. In the constructor of the subclass, if nothing is written, super() is given by default; it means to call the no-argument method of the parent class.
As shown in the figure: an output statement is written in the parent class, the subclass inherits the parent class, and there is a no-parameter construction method in the subclass, the system defaults to super() for the subclass, and then calls the subclass in the test classThe constructor will output the output statement of the parent class.



2. If the subclass constructor writes parameters, it will not use super() without parameters;

3. When creating a subclass, a constructor of the parent class must be called;
4. The super class constructor call must be the first statement of the sub class constructor;
Five. Usage of this keyword
1. this. accesses member variables of this class;
2. this. accesses member methods of this class ();
3. this(parameter); only other constructors of this class can be called, notitself, i.e. not recursive
(1) It must be the first statement (2) It must be written in the constructor (3) If this (parameter) is written, super() will not be given by default (4) The constructor cannot be recursive (5)this() and super() cannot exist at the same time
Six. The usage of super keyword
1.super. member method of parent class
2.super. member variable of classification


3.super (parameter)
Summary
When using the super keyword, you need to pay attention that the position must be the first, otherwise, if the subclass has parameters, it will call its own parameters and methods first, which will not achieve the purpose of inheriting the methods of the parent class.
边栏推荐
猜你喜欢
随机推荐
Mysql(四)
Typescript学习(一)
Jenkins Pipeline编写思路与技巧
布隆过滤器及LRU Cache的实现
0. About The Author And Preface
安装MySQL时出现starting the server失败
Codeforces Round #808 (Div. 2)||沉淀
架构实战营第九模块作业-毕业项目
WinServer 2019 组策略开启远程桌面
Mysql学习(一)
单臂路由与三层交换机实现跨VLAN通讯
为什么四个字节的float表示的范围比八个字节的long要广
辨析fork与vfork
Go语言基础(十一):反射
如何判断闰年
多线程相关:按序打印、交替打印FooBar、交替打印字符串
Heap series _0x04: Internal structure of heap (_HEAP=_HEAP_SEGMENT+_HEAP_ENTRY)
(一)BFC
认识盒子模型
超级火的夏日小空调









