当前位置:网站首页>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
边栏推荐
- 自定义mvc项目登录注册和树形菜单
- 基于redis6.2.4的redis cluster部署
- [OBS] frame loss and frame priority before transmission
- Getting started with easyUI
- 测试框架-unittest-跳过测试
- 【目标检测】YOLOv5跑通VisDrone数据集
- SAP Fiori 的附件处理(Attachment handling)
- HCIP笔记十一天
- What is chain game system development? How to make chain game system development
- Rainbow plug-in extension: monitor MySQL based on MySQL exporter
猜你喜欢

3D semantic segmentation - PVD

MySQL之联表查询、常用函数、聚合函数

为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台
![[target detection] yolov5 Runtong voc2007 dataset (repair version)](/img/b6/b74e93ca5e1986e0265c58f750dce3.png)
[target detection] yolov5 Runtong voc2007 dataset (repair version)

Rainbow plug-in extension: monitor MySQL based on MySQL exporter

气数已尽!运营 23 年,昔日“国内第一大电商网站”黄了。。。

多租户软件开发架构

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part4):结合问题分类的问题解析与检索语句生成

Fudan University emba2022 graduation season - graduation does not forget the original intention and glory to embark on the journey again

easyui下拉框,增加以及商品的上架,下架
随机推荐
Ilssi certification | the course of Six Sigma DMAIC
80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
气数已尽!运营 23 年,昔日“国内第一大电商网站”黄了。。。
复旦大学EMBA同学同行专题:始终将消费者的价值放在最重要的位置
复旦大学EMBA2022毕业季丨毕业不忘初心 荣耀再上征程
测试框架-unittest-跳过测试
Chain game development ready-made version chain game system development detailed principle chain game source code delivery
在 NgModule 里通过依赖注入的方式注册服务实例
[book club issue 13] +ffmpeg video capture function
[xiao5 chat] check the official account < the service provided by the official account has failed, please wait a moment>
第三章、数据类型和变量
WPF 实现用户头像选择器
在华为昇腾Ascend910上复现swin_transformer
IaaS基础架构云 —— 云网络
首页门户分类查询
从数字化到智能运维:有哪些价值,又有哪些挑战?
3D semantic segmentation - scribed supervised lidar semantic segmentation
Test framework unittest skip test
From digitalization to intelligent operation and maintenance: what are the values and challenges?
柏睿数据加入阿里云PolarDB开源数据库社区