当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
2022高教社杯 国赛数学建模 D题思路
如何设置边框圆角
2022高教社杯 国赛数学建模 B题思路
微信小程序学习(二)
Mysql学习(一)
Mysql学习(三)
设计一个登录小程序(while和getchar实现)
学编程的第八天
“泰迪杯”数据分析职业技能大赛B题 学生校园消费行为分析---复盘
4. Using Local Geospatial Data
认识盒子模型
Mysql(四)
Office365 AzureAD Intune 配置
学编程的第十天
前缀和相关:区域和检索 - 数组不可变、二维区域和检索 - 矩阵不可变...
架构实战营第九模块作业-毕业项目
九章云极DataCanvas APS机器学习平台获得中国信通院“领先级”评级
Typescript学习(一)
同步锁synchronized追本溯源
华为防火墙相关命令