当前位置:网站首页>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(){
}
}
边栏推荐
猜你喜欢
Fiddler(二)-手机抓包502错误解决方法
请你谈谈网站是如何进行访问的?【web领域面试题】
B站回应HR称“核心用户都是Loser”、求职者是“白嫖党”:已被劝退
一道[CSCCTF 2019 Qual]FlaskLight的详解再遇SSTI
JSP基本语法
从零开始的tensorflow小白使用指北
【正点原子STM32连载】第三章 开发环境搭建 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
inject() can only be used inside setup() or functional components.
[Punctuality Atom STM32 Serial] Chapter 4 STM32 First Experience Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]
随机推荐
去掉js代码文件所有注释
Thread类的基本使用。
今年37了,被大厂抢着要...
预测性维护学习之路
我和 TiDB 的故事 | TiDB 对我不离不弃,我亦如此
ISO14443A读卡流程(作为示例参考)
Wang Shuang's Assembly Language Chapter 4: The First Program
How to write patents are more likely to pass?
sync-diff-inspector 使用实践
如何从PG导入数据到kingbaseES
inject() can only be used inside setup() or functional components.
Libpq 是否支持读写分离配置
Shared_preload_libraries导致很多语法不支持
async - await
蘑菇书EasyRL学习笔记
王爽汇编语言第四章:第一个程序
区分惯性环节与延迟环节
redis分布式锁的实现
TiDB升级与案例分享(TiDB v4.0.1 → v5.4.1)
菲沃泰科创板上市:市值123亿 宗坚赵静艳夫妇身价76亿