当前位置:网站首页>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高压电工考试模拟100题及模拟考试
- 1.5 merge\rebase\revert\stash\branch
- [592. Fraction addition and subtraction]
- 看得清比走得快更重要,因为走得对才能走得远
- VR全景拍摄,助力民宿多元化宣传
- 2022高压电工考试模拟100题及模拟考试
- 【英语考研词汇训练营】Day 15 —— analyst,general,avoid,surveillance,compared
- (IROS 2022) 基于事件相机的单目视觉惯性里程计 / Event-based Monocular Visual Inertial Odometry
- 【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分
- Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022
猜你喜欢
随机推荐
Machine learning: self paced and fine tuning
LeetCode_406_根据身高重建队列
10. Learn MySQL like clause
2022 safety officer-b certificate examination simulated 100 questions and answers
IDC脚本文件运行
Map of China province > City > level > District > Town > village 5-level linkage download [2019 and 2021]
2022年危险化学品经营单位安全管理人员上岗证题目及答案
正负数值的正则表达式
Solution and implementation of APP accelerating reading and displaying IPFs pictures
【打包部署】
Vs2015 use dumpbin to view the exported function symbols of the library
Code management platform SVN deployment practice
An entry artifact tensorflowplayground
MQTT.js 入门教程:学习笔记
A perfect cross compilation environment records the shell scripts generated by PETA
【解决】ERROR in [eslint] ESLint is not a constructor
负数的十六进制表示
如何在多线程环境下使用 GBase C API ?
Setting of parameter configuration tool for wireless vibrating wire collector
IntelliJ IDEA 关联数据库









