当前位置:网站首页>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;
}
}
}
边栏推荐
- Magic Bracelet-【群论】【Burnside引理】【矩阵快速幂】
- 如何在多线程环境下使用 GBase C API ?
- 2022高压电工考试模拟100题及模拟考试
- 网络层的IP协议
- A perfect cross compilation environment records the shell scripts generated by PETA
- mysql主从架构 ,主库挂掉重启后,从库怎么自动连接主库
- 会议OA系统
- OpenShift 4 之AMQ Streams(1) - 多个Consumer从Partition接收数据
- OpenShift 4 - 使用 VerticalPodAutoscaler 优化应用资源 Request 和 Limit
- Informatics Olympiad all in one 1617: circle game | 1875: [13noip improvement group] circle game | Luogu p1965 [noip2013 improvement group] circle game
猜你喜欢
![Map of China province > City > level > District > Town > village 5-level linkage download [2019 and 2021]](/img/ea/fd799bbef5110fddf4066e76892f81.png)
Map of China province > City > level > District > Town > village 5-level linkage download [2019 and 2021]

An entry artifact tensorflowplayground

2022高压电工考试模拟100题及模拟考试

5 运算符、表达式和语句

阿里云服务器搭建和宝塔面板连接

2022安全员-C证特种作业证考试题库及答案

Get started quickly with flask (I) understand the framework flask, project structure and development environment

训练一个自己的分类 | 【包教包会,数据都准备好了】

蓝牙技术|2025年北京充电桩总规模达70万个,聊聊蓝牙与充电桩的不解之缘

Sentinel
随机推荐
完善的交叉编译环境记录 peta 生成的shell 脚本
3D全景展示新模式,成为破局的关键
VR全景拍摄,助力民宿多元化宣传
2022高压电工考试模拟100题及模拟考试
1.5 merge\rebase\revert\stash\branch
[English postgraduate entrance examination vocabulary training camp] day 15 - analyze, general, avoid, surveillance, compared
2022年安全员-B证考试模拟100题及答案
sql server 的关键字在哪张系统表?
oracle 创建用户且只有查询权限
看得清比走得快更重要,因为走得对才能走得远
v-bind指令的详细介绍
【英语考研词汇训练营】Day 15 —— analyst,general,avoid,surveillance,compared
Face warp - hand tear code
【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分
【C语言】详解顺序表(SeqList)
使用 GBase C API 执行存储过程是怎样的?
Linux initializes MySQL with fatal error: could not find my-default.cnf
IP protocol of network layer
一款入门神器TensorFlowPlayground
ES6 let与const