当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
第五章:可视化地理空间数据
(十)打包和项目部署
域控同步相关命令
第三章:GEE数据的使用(3.4-3.11)
第二章:创建交互式地图(2.1-2.3)
com.ctc.wstx.exc.WstxParsingException: Illegal character entity: expansion character
排序相关:数组的相对排序、最小的k个数(快排)、合并区间、翻转对 ...
Lens 创建 Service Accounts 及分配权限
2022华数杯B题思路: 水下机器人的组装计划
js和jq实现点击小眼睛后密码显示与隐藏切换
Go语言基础(十一):反射
求n的阶乘的两种方法
求素数的三种方法
vs2017下配置sqlite3环境
架构实战营第九模块作业-毕业项目
前缀和相关:区域和检索 - 数组不可变、二维区域和检索 - 矩阵不可变...
Dolphin Scheduler 2.x版本部署篇
yum安装mariadb数据库之后启动时提示 Failed to start mariadb.service: Unit not found
Go语言基础(十):接口
float属性的使用









