当前位置:网站首页>理解 继承、多态、抽象以及它们的概念
理解 继承、多态、抽象以及它们的概念
2022-07-17 00:14:00 【荼蘼_】
继承
1. 【访问修饰符】 class 子类名 extends 父类名{}
2. 类的继承
· 在java中定义一个类时,让这个类通过关键字 extends 继承一个已有的类。
· 被继承的类称为 父类(又称为 超类/基类),新的类被称为 子类 (又称为派生类)。
· 子类继承父类的所有属性和方法,也可以增加自己的属性和方法。
· 子类的访问修饰符的权限要大于等于父类的访问修饰符权限。
3. 规则
① 只支持单继承。每个类只能有一个父类,不允许有多重继承关系。
② 一个父类可以有多个子类。
③ 子类继承父类的所有属性和方法。
4. 子类的实例化
· 子类实例化时,先实例化父类,然后实例化子类。
· 调用父类的构造器时,先运行父类的构造器,父类构造器运行完毕才调用子类的构造器。
5. super()
· 子类继承父类时,并无参数传递的时候,在调用子类之前 super()可以省略不写,系统默认添加。(即程序自动先调用父类在运行子类代码)
· super()只能出现在子类的构造方法中,必须是第一行。
· super()的括号中的参数,调用父类哪个构造器,参数和构造器的传参类型一致。
· super()的例子对比可以参照【https://www.cnblogs.com/jesse-zhao/p/10660533.html】,我学习时理解就是找这个网址理解的。比较清晰简单,代码明确。
6. 构造器
① 子类不能继承父类的构造器。
② 子类可以在自己的构造器中调用父类的构造器。
super(参数1,参数2,...)
③调用父类中无参的构造器可以不写super()。
7. 访问权限修饰符的范围
private | default | protected | public | |
同一类 | √ | √ | √ | √ |
同一包中的类 | √ | √ | √ | |
不同包中的子类 | √ | √ | ||
其他包中的类 | √ |
================================================================================================
多态
1. 一种事物有多种形态叫做多态。
2. 对象 instance of 类 <-----理解:看对象是不是类的实例
如:父类转子类
3. 设置父类的引用类型/接口类型。
4. 多态的体现:
有继承或者实现
重写
父类引用指向子类对象
5. 多态的引用:
子类类型的对象赋值于父类类型的引用(通过父类实例化子类 / 通过父类中的对象调用子类的方法)
Anmial a = new Dog();
a.eat();
================================================================================================
抽象
1. 【访问修饰符】 abstract class 类名 {...}
2. 抽象类不能被实例化。
3. 有抽象方法的类一定是抽象类。(一个类中有一个/多个抽象方法,那么这个类一定是抽象类。)
4. 抽象类中的抽象方法在其子类中被实现,否则子类只能声明为abstract。
5. 有static的不是抽象方法。
6. 一个类部分实现接口的方法,该类一定是抽象类。
7. 一个类是抽象父类的子类,且 没有实现父类的所有抽象方法时,必须定义成抽象类。
边栏推荐
- Dueling DQN的理论基础及其代码实现【Pytorch + Pendulum-v0】
- STL--deque容器
- Visual studio 2019-qt debugging
- (with word operation and video explanation) map registration using ArcGIS_ Projection transformation_ General map making_ Thematic map making
- Chengxin University envi_ IDL third week class content 1: reading OMI data (HDF5 file) and output + parsing
- [tools] Application of SQLite local database in unity3d
- 【Unity编辑器扩展】快速定位资源和脚本的指定文件和路径
- Installing MySQL and JDBC on Windows
- Gdb+vscode for debugging 5 - GDB view relevant commands
- RuntimeError_ Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor)
猜你喜欢
攻防世界----easytornado笔记
flask模板注入
ENVI_IDL:读取所有OMI产品的NO2柱含量并计算月均值、季均值、年均值+解析
ENVI_ Idl: batch process the re projection of MODIS swath data and output it to GeoTIFF format + detailed analysis
【HDRP高清渲染管道】创建HDRP工程,把内置管线工程升级为HDRP工程
Visual Studio 2019-QT调试
Swagger——世界上最流行的Api框架
Logical vulnerability - authentication vulnerability
STL -- stack container
第2章-系统控制原理 -> 经典控制理论
随机推荐
【HSJFramework】Unity时间管理TimeManger计时器
工程编译那点事:Makefile和cmake(一)
SSTI template injection
[unity panel attribute literacy] set texture import settings after importing textures
STL -- string container
ENVI_ Idl: read the NO2 column content of all OMI products and calculate the monthly average, quarterly average, annual average + analysis
第2章-系统控制原理 -> 经典控制理论
(with word operation and video explanation) map registration using ArcGIS_ Projection transformation_ General map making_ Thematic map making
Clion installation and ROS development to realize automatic prompt completion
STL--set容器
leetcode力扣经典问题——42.接雨水
软件测试技术期中测试小结|软件测试基础&执行测试&测试设计和开发
STL--list容器(链表)
ENVI_ IDL: read OMI data (HDF5) and output it as GeoTIFF file + detailed parsing
元宇宙公链Caduceus项目详解(一):Caduceus Metaverse Protocol的项目理念及技术框架
【Unity面板属性扫盲】导入纹理后设置Texture Import Settings
STL--string容器
ENVI_IDL:批量拼接Modis Swath的逐日数据并输出为Geotiff格式
ENVI_ Idl: batch re projection of modisswath products (calling the secondary development interface) + parsing
STL -- stack container