当前位置:网站首页>Static and final keyword learning demo exercise
Static and final keyword learning demo exercise
2022-07-27 05:08:00 【if you never leave me, i will be with you till death do us apart】
The single interest model :
The singleton pattern (Singleton Pattern) yes Java One of the simplest design patterns in . This type of design pattern is a creation pattern , It provides the best way to create objects .
This pattern involves a single class , This class is responsible for creating its own objects , Also make sure that only a single object is created . This class provides a way to access its unique objects , You can directly access , There is no need to instantiate an object of this class .
Be careful :
- 1、 A singleton class can have only one instance .
- 2、 The singleton class must create its own unique instance .
- 3、 The singleton class must provide this instance to all other objects .
Details can be viewed rookie
static
Static Modifiers are used to create class methods and class variables .
1、Static Features of keywords :
(1)Static It's a modifier , Pertaining to members .
(2)Static Decorated members are shared by all objects .
(3)Static Prior to the existence of objects , because static Members of the class already exist with the loading of the class .
(4)Static There is more than one way to call the decorated members , Can be directly called by the class name . Class name , Static members .
(5)Static The decorated data is shared data , Objects store unique data .
final
1. keyword final Indicates final , Immutable .
2. keyword final You can modify variables 、 Method , There are also classes
3.final The decorated class cannot be inherited
4.final The decorated method cannot be overridden , Can't be rewritten
5.final Can't control the problem of whether it can be called , It means the last , Can't change , It can't be changed .
6.final The modified variable can only be assigned a value once
7.final Embellished references :
The reference can only point to 1 Objects , And it can only point to the object forever , And cannot point to other objects .
And during the execution of this method , After the reference points to the object , This object will not be collected by the garbage collection period
Until the end of the current method , To free up space .
8. although final The reference of points to the object A after , You can't point to objects again B,
But the object A Internal data can be modified .
9.final Decorated instance variable , Can only assign once . Conclusion : Because instance variables are not assigned manually , The system will assign a default value .
So demand final Decorated instance variables must be assigned manually . This manual assignment , Assigning values after variables can , In the constructor
You can also assign values in .( Display assignment 、 Constructor assignment 、 Code block assignment )
10.final Modified instance variables are generally added static modification
The ultimate conclusion :static final The jointly modified variable is called “ Constant ”, Constant names are recommended to be capitalized , Underline each word .
Constants are the same as static variables , The difference lies in : The value of a constant cannot be changed . The same thing is : Are stored in the method area , And are
Initialize when the class is loaded . Constants are generally public ,public modification .
Override parent method
describe
Parent class Base Several are defined in get Method , And one. sum Method ,sum The method is to sum a set of numbers . Please specify a subclass Sub Rewriting in getX() Method , bring sum Method returns x*10+y
Input description :
Integers
Output description :
Sum of integers
specific working means :
Sub yes Base Subclasses of , Therefore, it inherits the member variables and member methods of the parent class , In the member method ,getY() and sum() Because of the addition of final keyword , Cannot be modified , Subclasses are used directly , and getX() Functions can be found in subclasses , We rewrite it to get x The value of 10 times .
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
Sub sub = new Sub(x, y);
System.out.println(sub.sum());
}
}
}
class Base {
private int x;
private int y;
public Base(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public final int getY() {
return y;
}
public final int sum() {
return getX() + getY();
}
}
class Sub extends Base {
public Sub(int x, int y) {
super(x, y);
}
// Assign to first sub Of
@Override
public int getX() {
return super.getX() * 10;
}
Create singleton objects
边栏推荐
- "Photoshop2021 introductory tutorial" flatten "perspective images
- 2019 top tennis cup upload
- Inspiration from "flying man" Jordan! Another "arena" opened by O'Neill
- [search] two way search + A*
- The digital China Construction Summit ended with a list of massive pictures on site!
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- What is the future development direction of software testing engineers?
- 测试基础5
- static和final关键字 学习 demo练习
- 【搜索】双向广搜 + A*
猜你喜欢
随机推荐
Sub database and sub table
支付流程如何测试?
深入 Qt5 信号槽新语法
【搜索】双向广搜 + A*
35.滚动 scroll
Final Cut Pro Chinese tutorial (1) basic understanding of Final Cut Pro
自定义视口高度,多余的部分使用滚动
MQ message queue is used to design the high concurrency of the order placing process, the generation scenarios and solutions of message squeeze, message loss and message repetition
多态的详讲
Hiding skills of Photoshop clipping tool
STM32 Hal serial port (uart/usart) debugging experience (I) -- basic knowledge of serial port communication +hal library code understanding
听过最自律的一句话: 那些我难以言表 不作声响
Three paradigms, constraints, some keyword differences,
Web框架介绍
Invert a Binary Tree
Reproduce ssa-gan using the nine day deep learning platform
一道数学题,让芯片巨头亏了5亿美金
Installation and template setting of integrated development environment pychar
[search] connectivity model of DFS + search order
【无标题】按照一定的条件,对 i 进行循环累加。条件通常为循环数组的长度,当超过长度就停止循环。因为对象无法判断长度,所以通常搭配 Object.keys() 使用。\nforEach 一般认为是 普









