当前位置:网站首页>Chapter VI succession
Chapter VI succession
2022-07-25 16:53:00 【Take charge of [email protected]】
1、 The basic syntax of inheritance
**【 brief introduction 】:** stay Java Use in extends Keyword to indicate that one class inherits another class .
【 grammar 】:
public class Sub extends Base{
}
【 Case presentation 】:
public class Base {
public int a;
private int b;
int c;
protected int sum(int a, int b) {
return a + b;
}
}
public class Sub extends Base {
public void sub() {
a = 2;
b = 2;
c = 2;
sum(1, 2);
}
}
【 Graphical error 】:
【 matters needing attention 】:
- When Sub and Base When in the same package ,Sub Class inheritance Base Class public、protected、 And the member variables and methods of the default access level .
- When Sub and Base When not in the same package ,Sub Class inheritance Base Class public、protected Access level member variables and methods .
- A class can only have one direct parent class , Cannot inherit multiple parent classes directly at the same time , But you can indirectly inherit multiple parent classes .
- Each class inherits by default Object class .
- The constructor of a parent class cannot be inherited by a child class ( Constructor must be the same as class name )
2、 Method overloading (Overload)
【 Application scenarios 】: There are many ways to realize the function of class , Choose that way , Depends on the parameters given by the caller .
【 Meet the conditions of overload 】:
- Same method name
- Parameter types in methods 、 Number 、 At least one item in the order is different
- The return value types of methods can be different .
- The modifiers of methods can be different
3、 Methods cover (Override)
【 Application scenarios 】: When the method in the parent class does not meet the requirements of the child class , You need to override the methods in the parent class .
【 Method covers the constraints that are satisfied 】:
- Name of subclass method 、 Parameter signature 、 And return value types must be the same as the parent class .
【 Correct code demonstration 】:
public class Base {
public void method() {
}
}
public class Sub extends Base {
public void method() {
}
}
【 error analysis 】:
- A subclass method cannot reduce the access rights of a parent method .( reason : Conflicts with polymorphic mechanisms .)
【 Error code demonstration 】:
public class Base {
public void method() {
}
}
public class Sub extends Base {
private void method() {
// Subclasses narrow the access rights of the methods of the parent class
}
}
【 error analysis 】:
- Subclass methods cannot throw more exceptions than the parent class .( Cause program to terminate abnormally )
- Method overrides only exist between subclasses and superclasses , Methods in the same class can only be overloaded and cannot be overridden .
- A static method of a parent class cannot be overridden as a non static method by a child class .
【 Error code demonstration 】:
public class Base {
public static void method() {
}
}
public class Sub extends Base {
public void method() {
// Compilation error
}
}
【 error analysis 】:
- Subclasses can define static methods with the same name as the static methods of the parent class , In order to hide the static method of the parent class in the subclass .
【 Correct code demonstration 】:
public class Base {
public static void method() {
}
}
public class Sub extends Base {
public static void method() {
}
}
- Private methods of a parent class cannot be overridden by subclasses
- Abstract methods of a parent class can be overridden by subclasses in two ways (1、 Abstract methods that implement the parent class 2、 Redeclare the abstract methods of the parent class )
【 Correct code demonstration 】:
public abstract class Base {
abstract void method1();
abstract void method2();
}
public abstract class Sub extends Base {
// Realize and expand access
public void method1() {
}
// Redeclare the abstract methods of the parent class , It didn't happen
public abstract void method2();
}
- Non abstract methods of the parent class can be overridden as abstract methods .
public class Base {
public void method() {
}
}
public abstract class Sub extends Base {
public abstract void method();
}
4、 The difference between method coverage and method overloading
【 The same thing 】:
- All require the method to have the same name
- Can be used between abstract methods and non abstract methods
【 Difference 】:
- Method coverage requires that parameter signatures must be consistent , Method overloading requires that parameter signatures must be inconsistent
- Method override requires that the return value type must be consistent , Method overloading has no limit
- Method overrides occur between subclasses and superclasses , Method overloading is used for all methods in the same class
- Method coverage has certain restrictions on the access rights of methods and the exceptions thrown , Method overloading has no restrictions
- Methods of a parent class can only be overridden by subclasses once , Overloading can be repeated
5、super keyword
- super Keyword indicates the parent of the current subclass
- super Keyword can be used to access the constructor of the parent class , The default subclass will access the parameterless constructor of the parent class .
- Access non private fields and methods of the parent class ( The method here can only be non static )
6、 polymorphic
【 Definition 】:
Receive subclass objects with parent type or parent interface . That is, multiple states of an object at runtime .
【 Precautions for use 】:
- For variables of reference type ,Java The compiler will process according to the type it declares . for example : If you receive a subclass object with a parent type , When you need to access the unique attributes in the subclass , You must cast the object to a subclass type , Otherwise, compile and report an error .
- For variables of reference type , Runtime 、Java The virtual opportunity is handled according to the object it actually refers to .
- In a runtime environment , When accessing the methods and properties of the referenced object by referencing type variables ,Java The virtual machine adopts the following binding rules :
- The instance method is bound to the method of the object actually referenced by the reference variable , This kind of binding belongs to dynamic binding , Because it is run-time by Java Dynamically determined by the virtual machine .
- The static method is bound to the method of the type declared by the reference variable , This kind of binding belongs to static binding , Because actually binding has been done in the compilation stage .
- Member variables ( Including static variables and instance variables ) And the member variable of the type declared by the reference variable
版权声明
本文为[Take charge of [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/206/202207251650542758.html
边栏推荐
- Getting started with easyUI
- Rebudget: balance efficiency and fairness in market-based multi-core resource allocation by reallocating the budget at run time
- 一百个用户眼中,就有一百个QQ
- LVGL 7.11 tileview界面循环切换
- 【读书会第13期】+FFmpeg视频采集功能
- easyui下拉框,增加以及商品的上架,下架
- 【小5聊】公众号排查<该公众号提供的服务出现故障,请稍后>
- Rosen's QT journey 100 QML four standard dialog boxes (color, font, file, promotion)
- 第三章、数据类型和变量
- 测试框架-unittest-跳过测试
猜你喜欢

动态规划题目记录

柏睿数据加入阿里云PolarDB开源数据库社区

企业直播风起:目睹聚焦产品,微赞拥抱生态

【目标检测】TPH-YOLOv5:基于transformer的改进yolov5的无人机目标检测

Exception handling mechanism topic 1

搜狗批量推送软件-搜狗批量推送工具【2022最新】

Jenkins' role based authorization strategy installation configuration

Replicate swin on Huawei ascend910_ transformer

Dynamic planning topic record

Budget report ppt
随机推荐
一百个用户眼中,就有一百个QQ
[cloud co creation] explore how gaussdb helps ICBC create core financial data
Go语言系列:Go从哪里来,Go将去哪里?
测试框架-unittest-跳过测试
slf4j 搭配 log4j2 处理日志
[mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart
如何安装govendor并打开项目
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part4):结合问题分类的问题解析与检索语句生成
Rosen's QT journey 100 QML four standard dialog boxes (color, font, file, promotion)
基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
微信公众号开发之消息的自动回复
动态规划题目记录
MySQL linked table query, common functions, aggregate functions
MySQL view
从数字化到智能运维:有哪些价值,又有哪些挑战?
GTX1080Ti 光纤HDMI干扰出现闪屏1080Ti 闪屏解决方法
Wechat applet does not use plug-ins, rendering videos in rich text, image adaptation, plus version
【obs】转载:OBS直播严重延迟和卡顿怎么办?
吴恩达逻辑回归2
ILSSI认证|六西格玛DMAIC的历程