freeCodeCamp/guide/chinese/java/final-keyword/index.md

60 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Final
localeTitle: 最后
---
## 最后
您使用`final`关键字来标记变量常量以便只能分配一次。因此您必须使用值初始化最终变量。如果未初始化在声明时在Constructor内部或内部静态块中将发生编译时错误。
**_例_**
```java
class MyClass {
public static final double PI = 3.14;
public static void main(String[] args){
System.out.println(PI);
}
}
```
PI现在是一个常数。任何为其赋值的尝试都会导致错误。
* * *
如果您将任何方法设为最终方法,则无法覆盖它。
```java
class Bike{
final void run(){System.out.println("running");}
}
class Honda extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
```
输出将是 - 输出:编译时错误
* * *
如果您将任何课程作为最终成绩,则无法对其进行扩展。
```java
final class Bike{}
class Honda1 extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda1 honda= new Honda();
honda.run();
}
}
```
输出将是 - 输出:编译时错误