当前位置:网站首页>Kotlin - inherit open
Kotlin - inherit open
2022-04-23 00:35:00 【A lazy programmer】
- Kotlin The parent of all classes in is Any(Java Medium is Object), It provides three functions by default :equals()、hashCode()、toString().
- General class ( That is, non abstract classes ) Default cannot be inherited ( amount to Java Is declared as final Class ). Use open Keyword modified classes can be inherited , Then use the functions and attributes that are allowed to be overridden in the class open modification ( Otherwise, duplicate functions or attributes are not allowed in subclasses , Function overloading is not affected ( The function has the same name, but the parameter list is different ) ). Use... In subclasses override A key modifier is a function or attribute that is overridden .
How to write it
- And Java Similarly, before calling its own construction, a class must first access the construction of its parent class for initialization ( First father, then son ).Kotlin Inheritance is also written using colons , Like declaring variables, the type of a class is the parent class , It can also be understood that the main construction of the child class delegates initialization to the main construction of the parent class , There's no need to look like Java Do the same thing. extends come out .
- If a subclass has a primary construct, it delegates the primary construct of the parent class to initialize , Declaring a secondary construct to delegate a primary construct is equivalent to indirectly delegating a parent construct . If the subclass has no main structure , Declare that this construct will use super The primary construct or secondary construct delegated to the parent class is initialized .
| Parent class | Empty parameter main structure ( Show or implicitly declare ) open class Father { } open class Father( ) { } |
There are main structures with parameters open class Father(name: String) |
Ownerless structure ( The primary construct is not declared, but the secondary construct is declared ) open class Father { constructor(name: String) } |
| Subclass There is no main structure |
Subclasses have no primary constructor , Declare that the secondary construct can not display delegates to the parent class super() |
Subclasses have no primary constructor , Declare that the next construction should be delegated to the parent class super() initialization |
|
| class Son : Father{ constructor( ) constructor(str: String) } |
class Son : Father { // In this way, subclasses can create parameterless instances constructor() : super("") constructor(str: String) : super(str) } |
class Son : Father { // Only when the parent class has empty parameter construction can the child class have empty parameter construction // If the parent class has no empty parameter structure , In this way, you can create an empty parameter instance constructor() : super("") constructor(str: String) : super(str) } |
|
| Subclass It shows the main structure |
Subclass declares that the secondary construction should be delegated to the primary construction , Cannot be a parent class super(). |
||
| class Son( ) : Father( ) | class Son( ) : Father(" ") class Son(str: String) : Father(str) |
class Son( ) : Father(“ ”) class Son( str: String) : Father(str) |
|
| Subclass There is an implicit main structure ( It is recommended to write as ) |
In this case, it is impossible to define a secondary construct , Once defined, there is no implicit main construct , Otherwise an error : It is impossible to initialize a parent class without a main constructor . |
||
| class Son : Father( ) | class Son : Father(" ") | class Son : Father(" ") | |
Overrides of attributes in the main construct
Subclass main construction can have The name and type of the parameter The same as in the parent class's main construct ( Even if... In the parent class's main construct Parameters of the same name and type Used open、var/val Modifiers do not affect ). If the parameter in the main construction of the subclass uses var/val Decoration won't work , Unless... In the parent class's main construct Parameters of the same name and type Use open Allow to override , At the same time, use var/val modification ( Only when it becomes an attribute can there be an override ), Subclasses should also use override modification . Note that subclasses cannot be used var Cover val, After all, subclasses can only be narrowed, not expanded .
版权声明
本文为[A lazy programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230033546060.html
边栏推荐
- C# 11 对 ref 和 struct 的改进
- CAS unified identity authentication (III): external independent configuration
- C console application flash when running solution
- L3-1 直捣黄龙 (30 分) djk+dfs
- 网线IP配置
- ArcGIS 城市生活区用地适宜性评价(四)
- 33岁,工作十年被裁员,所谓经验根本不值钱
- L2-022 重排链表 (25 分) 链表操作
- ArcGIS urban living area land suitability evaluation (V)
- ArcMAP 使用绘图工具添加注记
猜你喜欢
![[essay contest] the first essay contest of tidb community column. Come and gather all the surrounding areas at one time!](/img/b9/7ab9105257facadf849c144b3491cb.jpg)
[essay contest] the first essay contest of tidb community column. Come and gather all the surrounding areas at one time!
![[image classification] understand Alex net](/img/a5/fb134430ae18fcc85c6746f5750b1d.jpg)
[image classification] understand Alex net

Ten years of working at Google is also a happy decade for me

Installing MySQL in centos7

层次分析法软件操作步骤(yaahp)

Steps to apply for a CA certificate

API post, the interface document generation tool, is amazing

【AI视野·今日Sound 声学论文速览 第四期】Thu, 21 Apr 2022

ArcGIS urban living area land suitability evaluation (V)

Usage of while (n -- > 0)
随机推荐
ArcGIS urban living area land suitability evaluation (III)
ArcGIS 城市生活区用地适宜性评价(一)
湖泊遥感研究进展(概述)
Beifu el5101 module obtains the feedback position of the grating ruler and binds it to the NC axis configuration
Pain points solved by tidb under the wave of localization
【征文大赛】TiDB 社区专栏第一届征文大赛,快来一次性集齐所有周边吧!
Calculation of Beifu scaling factor
Migrate AWS S3 data to tidb cloud cluster
396. Rotation function / Sword finger offer II 013 Sum of two-dimensional submatrix
Kotlin - 继承 open
Install authorizationpolicy and envoyfilter
[C#]给地球点颜色看看
ArcGIS urban living area land suitability evaluation (II)
EXCEL IF函数的简单使用
Beifu el6631 and Siemens 1200 do PROFINET master-slave communication
层次分析法软件操作步骤(yaahp)
Detailed introduction of Mitsubishi mr-je-c servo application
C# 11 对 ref 和 struct 的改进
According to this effective method, UI automation testing is so simple
Acwing spring daily question - do you know ABC