当前位置:网站首页>Final keyword and enumeration type
Final keyword and enumeration type
2022-07-28 09:22:00 【lilshork】
Catalog
final matters needing attention
final What is it?
final The surface meaning of is unchangeable 、 Constant quantity 、 The final 、 Immutable meaning , It refers to the amount that cannot be changed .
stay java in final The modifier is a constant , And the variable name should be capitalized .
for example :
public static final double PI = 3.14;
final How to use it?
① Modifying variables , By final Decorated variables must be initialized , After the initial value is assigned, it cannot be reassigned .
final int x1= 10000;
② Modification methods , By final The decorated method cannot override .
Created an animal class , There are ways to eat and sleep , The method of sleeping is final modification .
public class Animal {
public void eat(){
System.out.println(" Animals eat ");
}
public final void sleep(){
System.out.println(" Animals sleep ");
}
}Then create a dog class to inherit animal class .
public class Dog extends Animal{
}Then override the method of the parent class in the dog class

You can see , Dogs can only treat people who are not final Modified method rewriting , But you can call the parent class that is final The method of decoration .
public class Dog extends Animal{
@Override
public void eat() {
System.out.println(" Dogs eat ");
}
}public class AnimalTest {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sleep();
dog.eat();
}
}
③ decorator , By final Modified class , uninheritable .
④ Modify local variables , Variable can only be assigned once .
⑤ Decorate instance member variables , It can only be assigned once and must be assigned .
final matters needing attention
① By final The name of the modified constant , Generally, there are writing standards , All letters are capitalized .
② The variables in the interface are public static final Of .
③ Be careful not to final And finally、finalize() Wait to get confused .
What are enumeration types
stay Java in , By enum The type of keyword decoration is enumeration type , It's a special type of data , The reason why it is special is that it is not only a kind of type, but also has some special constraints more than a kind of type , Therefore, the existence of these constraints also creates the simplicity of enumeration types 、 Security and convenience . Enumeration is generally used in the following cases : Define constants 、 Add new method 、 And switch Use a combination of 、 Implementation interface 、 Use interfaces to organize enumerations .
Format of enumeration type
public enum Enum name {
Enumerate instances , These instances write capital names directly .
}
for example :
public enum Sex{
BOY,GIRL;
}
Examples of enumeration types
Enumeration type and switch Comprehensive application of :
Create an enumeration type week class :
public enum Week {
MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY
}Create a test class , There is a way to show the day of the week .
public class WeekTest {
public static void main(String[] args) {
show(Week.MONDAY);
}
public static void show(Week week){
switch (week){
case MONDAY:
System.out.println(" Monday ");
break;
case TUESDAY:
System.out.println(" Tuesday ");
break;
case WEDNESDAY:
System.out.println(" Wednesday ");
break;
case THURSDAY:
System.out.println(" Thursday ");
break;
case FRIDAY:
System.out.println(" Friday ");
break;
case SATURDAY:
System.out.println(" Saturday ");
break;
case SUNDAY:
System.out.println(" Sunday ");
break;
}
}
}
边栏推荐
- 2022年安全员-B证考试模拟100题及答案
- 【一花一世界-郑一教授-繁简之道】可解释神经网络
- 3D全景展示新模式,成为破局的关键
- Realize batch data enhancement | use of keras imagedatagenerator
- 修改虚拟机IP地址
- 从开发转测试:我从零开始,一干就是6年的自动化测试历程
- F - Jealous Two-二维逆序对
- ES6 let与const
- From development to testing: I started from scratch and worked for six years of automated testing
- Sword finger offer
猜你喜欢

Sentinel

MQTT.js 入门教程:学习笔记

CSV file storage

【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分

Bluetooth technology | the total scale of charging piles in Beijing will reach 700000 in 2025. Talk about the indissoluble relationship between Bluetooth and charging piles
![[附下载]推荐几款暴力破解和字典生成的工具](/img/c6/f4a9c566ff21a8e133a8a991108201.png)
[附下载]推荐几款暴力破解和字典生成的工具

推荐一个摆脱变量名纠结的神器和批量修改文件名方法

The chess robot pinched the finger of a 7-year-old boy, and the pot of a software testing engineer? I smiled.

51单片机存储篇:EEPROM(I2C)

网络层的IP协议
随机推荐
01 tensorflow calculation model (I) - calculation diagram
JSON file storage
js数组去重,id相同对某值相加合并
Vs2015 use dumpbin to view the exported function symbols of the library
2022 high voltage electrician examination simulated 100 questions and simulated examination
MySQL 8.0.30 GA
Dapp安全总结与典型安全事件分析
Principle of line of sight tracking and explanation of the paper
Sentinel
2022安全员-C证特种作业证考试题库及答案
正则表达式为十六进制数字?
看得清比走得快更重要,因为走得对才能走得远
Kubernetes cluster configuration serviceaccount
Bluetooth technology | the total scale of charging piles in Beijing will reach 700000 in 2025. Talk about the indissoluble relationship between Bluetooth and charging piles
How to obtain the subordinate / annotation information of KEGG channel
canvas常用原型方法及绘制图片应用
面经-手撕代码
CakePHP 4.4.3 发布,PHP 快速开发框架
Feign调用异常[Running, pool size = 10, active threads = 10, queued tasks = 0, completed tasks = n]
Prometheus TSDB analysis