当前位置:网站首页>Inheritance and the static keyword
Inheritance and the static keyword
2022-08-04 09:05:00 【fourteen seventeen】
目录
面向对象的三大特征之二:继承
继承概述、使用继承的好处
什么是继承? Javaallow a class to passextendsEstablish a parent-child relationship with another class,这就是继承.
继承的格式:
子类 extends 父类
继承的特点:
子类继承父类后,You can get the properties and behavior of the parent class.
Core Benefits of Inheritance:
提高代码的复用性,The same code for multiple subclasses can be placed in the parent class,Enhanced class extensibility.
继承的设计规范、内存运行原理
设计规范:Parents common attributes and behaviors in children,Subclass-specific properties and behaviors are placed in the subclass itself.
内存原理:很重要.
继承的特点
子类可以继承父类的属性和行为,但是子类不能继承父类的构造器.
Can a subclass inherit the parent class's private members??有争议的,我认为可以,只是不能直接访问.
Subclasses can directly use the subclass name to access the static members of the superclass.Does the subclass inherit the static members of the parent class??? I think there is no inheritance,Only can be Shared to access the parent class static members,共享并非继承.
Java是单继承模式:一个类只能继承一个直接父类.
Java不支持多继承、但是支持多层继承.
Java中所有的类都是Object类的子类.
继承后:成员变量、成员方法的访问特点
就近原则
this.子类自己的成员变量
How to access parent class member in subclass method??? super.父类成员变量/父类成员方法
继承后:方法重写
概念:A subclass overrides the statement is the same as the parent class method,覆盖父类的方法.The subclass thinks that the method of the parent class is not easy to use,Way to use your rewritten in the future.
Overriding the method is recommended to add a rewrite verification annotation:@Override
作用:The requirement must be rewritten correctly to not report an error
作用2:可读性好
重写的要求:
The name and parameter list of the overridden method must match the name and parameter list of the overridden method of the parent class(重点)
私有方法不能重写
静态方法不能重写
重写方法的权限 >= 被重写方法的访问权限.
继承后:子类构造器的特点
特点:By default, all constructors of subclasses will first access the no-argument constructor of the parent class,Execute your own constructor
why? father before son. First call its father's constructor to initialize the data of the parent class,Then call your own constructor to initialize your own data.
代码层面:The first line of the default subclass constructor has asuper() Access the no-argument constructor of the parent class,写不写都有
继承后:子类构造器访问父类有参构造器
Call the parent class with parameter constructor,初始化继承自父类的数据.
super(....) Call the parent class constructor with parameters
this、super使用总结
thisaccess the members of the current object of the subclass.
super:Specify access to members of the parent class in a subclass method.
this(...) : 访问本类兄弟构造器
super(...):In the constructor of this class, specify the constructor that accesses the parent class
注意事项:super(...) 必须放在第一行,this(...) must also be on the first line.因此2cannot coexist in a constructor.
static关键字
是什么? 静态的意思,可以修饰成员变量,可以修饰成员方法
staticClassification of Modified Member Variables
静态成员变量
有static修饰,属于类,与类一起加载,内存中只有一份,可以被共享访问.
什么时候用呢?If information is to be shared,Just need one,比如:系统在线人数.
访问规则:
建议用类名访问:类名.静态成员变量
注意:同一个类中,To access static members, you can omit the class name and do not write.
In theory, it is also possible to access this way:对象名.静态成员变量(不推荐)
实例成员变量
无static修饰,属于对象,An instance member variable exists in every object.比如:name age
什么时候用呢?比如:name age,This information is present in every object,And the information is not at the same time,Defined as an instance member variable.
访问规则:
只能用:对象名.实例成员变量.
static修饰成员方法
静态成员方法
有static修饰,属于类,与类一起加载,可以被共享访问.
什么时候用呢?If it is a general function.
访问规则:
建议用类名访问:类名.静态方法
注意:同一个类中,To access static members, you can omit the class name and do not write.
In theory, it is also possible to access this way:对象名.静态成员方法(不推荐)
实例成员方法
无static修饰,属于对象,Access must be triggered with an object.
什么时候用呢?if this method belongs to the object,And to directly access the instance members of the object,declared as an instance method.
访问规则:
只能用:对象名.实例成员方法.
static的应用知识
工具类
是什么? 里面都是静态方法,In order to complete a common function
好处:一次编写,Adjustable everywhere,提高代码的复用,提高开发的效率.
注意:It is recommended to privatize the constructor of the utility class,Because the tool class does not need to create objects externally,它的方法都是静态方法,直接用类名访问即可.
代码块
是什么? 类的5大成分(成员变量、成员方法、构造器、代码块、内部类)
分类
静态代码块:(重点了解一下)
格式:static {}
特点:属于类,与类一起优先加载,Automatic trigger execution once.
作用:can be used at program startup,Initialize static resources.
实例代码块(几乎不用):
格式:{}
特点:属于对象的,Executed every time the constructor is called before building the object,再执行构造器.
作用:Can be used to initialize instance resources
单例
单例是一种设计模式,是为了解决某个问题
单例解决:Ensure that a class can only produce one object externally.
The focus is on how to implement a singleton:
饿汉单例
特点:拿对象时,对象已经存在
实现步骤:
1、构造器私有
2、Define a static member variable to store an object.
代码
/**
Implementing singleton classes using hungry singletons
*/
public class SingleInstance {
/**
2、饿汉单例是在获取对象前,对象已经提前准备好了一个.
这个对象只能是一个,So define static member variables remember.
*/
public static SingleInstance instance = new SingleInstance();
/**
1、必须把构造器私有化.
*/
private SingleInstance(){
}
}
懒汉单例
特点:when taking the object,just start creating an object.
实现步骤:
1、构造器私有
2、定义静态成员变量,No object currently exists.
代码
/**
懒汉单例
*/
public class SingleInstance2 {
/**
2、Define a static member variable responsible for storing an object.
只加载一次,只有一份.
注意:最好私有化,这样可以避免给别人挖坑!
*/
private static SingleInstance2 instance;
/**
3、提供一个方法,对外返回单例对象.
*/
public static SingleInstance2 getInstance() {
if(instance == null){
// 第一次来拿对象 :此时需要创建对象.
instance = new SingleInstance2();
}
return instance;
}
/**
1、私有化构造器
*/
private SingleInstance2(){
}
}
边栏推荐
- 线程安全问题
- MATLAB/Simulink快捷键
- spark算子讲解
- 94后字节P7晒出工资单:狠补了这个,真不错...
- 将jpg图片转换成yuv420(NV12)数据文件
- Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]
- DNS 查询原理详解—— 阮一峰的网络日志
- ShowMeAI —— Show u 三连
- The difference between character stream and byte stream
- Recommend several methods that can directly translate PDF English documents
猜你喜欢
Anton Paar安东帕密度计比重计维修DMA35性能参数
PD 源码分析- Checker: region 健康卫士
【云驻共创】HCSD 大咖直播–就业指南
线程安全问题
从零开始的tensorflow小白使用指北
他97年的,我既然卷不过他...
云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
TCP的四次挥手
Yolov5 replaces the backbone network of "Megvii Lightweight Convolutional Neural Network ShuffleNetv2"
Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]
随机推荐
About Oracle RAC 11g rebuilding the disk group
GBsae 8c 数据库使用中报错,肿么办?
PD 源码分析- Checker: region 健康卫士
云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
【STM32】STM32F103系列名称与封装、内存
Wang Shuang's Assembly Language Chapter 4: The First Program
MATLAB绘图总结
ansible部署脚本--亲测可用无坑
从零开始C语言精讲篇6:结构体
Grafana9.0发布,Prometheus和Loki查询生成器、全新导航、热图面板等新功能!
发现WRH几个表被锁了,怎么办?
Implementation of redis distributed lock
tcp连接的细节
【论文笔记】Understanding Long Programming Languages with Structure-Aware Sparse Attention
async - await
思想茶叶蛋 (Jul 31,2022)| 元宇宙(Metaverse)下了一枚什么样的蛋
oracle sql 多表查询
sync-diff-inspector 使用实践
王爽汇编语言第四章:第一个程序
Fiddler(二)-手机抓包502错误解决方法