当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
Gin框架(一):基础概览
2022华数杯A题 B题 C题 思路汇总
如何设置边框圆角
Win10 安装系统跳过创建用户,直接启用 administrator
同步锁synchronized追本溯源
ARM基础知识点笔记
WinServer 2019 组策略开启远程桌面
C语言的常量和操作符
文字样式的常见属性的如何使用?
Codeforces Round #808 (Div. 2)||沉淀
WinServer 2019 组策略删除本地管理员且只允许域用户登陆
QT页面跳转的实现
投入C语言
web项目访问jar内部的静态资源
设计一个登录小程序(while和getchar实现)
Anatomy of Storage Size, Value Range, and Output Format of Basic Data Types in C Language
2022年深圳杯建模A题思路: 破除“尖叫效应”与“回声室效应”,走出“信息茧房”
学习编程的第二天
Ntdsutil 转移主域控五大角色
选择器的使用