当前位置:网站首页>023.static and final use traps
023.static and final use traps
2022-07-18 12:33:00 【kfdbes】
Lesson 23
- static Modification methods :static The decorated method is called static method . For static methods , You can use the class name . Method name to access .
normal
package exercise;
public class exercise006 {
public static void main (String[] args){
MyStatic2 test =new MyStatic2();
test.out();
}
}
class MyStatic2{
public void out(){
System.out.println("out");
}
}

Revised
package exercise;
public class exercise006 {
public static void main (String[] args){
MyStatic2.out();
}
}
class MyStatic2{
public static void out(){
System.out.println("out");
}
}

contrast
package exercise;
public class exercise007 {
public static void main (String[] args){
N n = new N();
n.output();
}
}
class M{
public static void output(){
System.out.println("m");
}
}
class N extends M{
public static void output(){
System.out.println("N");
}
}

package exercise;
public class exercise007 {
public static void main (String[] args){
M m = new N();
m.output();
}
}
class M{
public static void output(){
System.out.println("m");
}
}
class N extends M{
public static void output(){
System.out.println("N");
}
}

2. Static methods can only inherit , Can not rewrite (Override).
3. final keyword :final You can modify properties 、 Method 、 class .
4. final decorator : When a class is final When it is modified , Indicates that the class is a terminal class , That is, it cannot be inherited .
package exercise;
public class exercise008 {
public static void main(String[] args){
F f = new F();
}
}
final class E{
// Indicates that the class cannot be inherited
}
class F extends E{
}

5. final Modification methods : When a method is final When it is modified , Indicates that the method is a final state method , That is, it cannot be rewritten (Override) .
The wrong sample
package exercise;
public class exercise008 {
public static void main(String[] args){
H h =new H();
h.out();
}
}
class G{
public final void out(){
System.out.println("G");
}
}
class H extends G{
public void out(){
System.out.println("H");
}
}

6. final Modify properties : When an attribute is final When it is modified , Indicates that the property cannot be overridden .
The wrong sample
package exercise;
public class exercise008 {
public static void main(String[] args){
People people = new People();
people.age = 20;
}
}
class People{
final int age =10;
}

package exercise;
public class exercise008 {
public static void main(String[] args){
People people = new People();
people.address.name = "shanghai";
}
}
class People{
final Address address = new Address();
}
class Address{
String name = "dalian";
}
It can be executed 
7. When final When decorating a native data type , The value representing the native data type cannot be changed ( For example, you can't 10 become 20); If final When modifying a reference type , Indicates that the reference type cannot point to other objects ; But the content of the object that the reference refers to can change .
边栏推荐
- [wechat applet] slide effect realization
- Elementary C language - structure
- ThreadX内核源码分析(SMP) - 线程执行核remap
- How to view RT thread documents, engineering establishment of RT and rapid construction of BSP
- Redis 中两个字段排序
- Transformation of brushless motor on disk
- Rust language -- xiaoxiaobai's introductory learning 10
- Introduction, installation and use of terrain command line tools
- Regular database backup Linux
- Rust语言——小小白的入门学习10
猜你喜欢

PostgreSQL weekly person interview
![[wechat applet] simple and easy-to-use icon (94/100)](/img/2b/11ca1196531fe837ba42142491e896.png)
[wechat applet] simple and easy-to-use icon (94/100)

The value of applet container technology in hybrid hybrid app development

openEuler 知:repo

Sorting of two fields in redis

Interface automated testing: postman practical tutorial

三个步骤,一天就搞定了MySQL,让我顺利拿下了天猫offer

How to view RT thread documents, engineering establishment of RT and rapid construction of BSP

PostgreSQL每周人物采访

Why is private domain operation so popular? How can enterprises do their private domain operations well?
随机推荐
Summary of autumn recruitment problems
三個步驟,一天就搞定了MySQL,讓我順利拿下了天猫offer
AcWing 395. 冗余路径 题解(无向图的双连通分量)
What is the whole process of Celsius' bankruptcy? This article takes you personally
Write the shader in the later stage of posteffect
0714下午1,review,
En trois étapes, j'ai terminé MySQL en une journée, ce qui m'a permis d'obtenir l'offre de tmall en douceur.
长安链介绍-02
技术分享| 快对讲-5G对讲
0x22, 0x2e services of UDS
Terraform命令行工具介绍、安装、使用
Redis 中两个字段排序
Detailed explanation of assembly language programming skills (with examples)
[C language] structure, enumeration, union
Top100国内NFT平台 联盟链、公链使用情况统计
C# 使用JObject解析嵌套json
ThreadLocal夺命11连问 我实在扛不住呀
[object conversion] vo2dto use
HDOJ-2057(A + B Again)
openEuler 知:管理策略