JAVA using an object-oriented -final keyword

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_42947972/article/details/102472074

final keyword:

1.final是一个可以修饰符,可以修饰类,方法,变量.
2.final修饰的类不可以被继承
3.final修饰的方法不可以被覆盖
4.final修饰的变量是一个常量,只能赋值一次
  class Fu1{
    void method()
    {
        //假设这里是调用了底层系统的资源
    }
}
class Zi1 extends Fu1{
    void method()
    {
        System.out.println("haha");
        //这里把Fu类中的方法method()覆盖了,导致整个都崩了
    }
}

All required final turn it into not covered.
Modification can not be inherited class is similar empathy ...

class Fu1{
    final void method()
    {
        //假设这里是调用了底层系统的资源
    }
}
class Zi1 extends Fu1{
    void method()//如果这里想覆盖就会报错了!
    {
        System.out.println("haha");
    }
}

Why modified final variables?

 其实在程序如果一个数据是固定的,那么直接使用这个数据就可以了.
   但这样阅读性差,所以它给数据起个名称.
   而且这个变量名称的值是不能变化的,所以加上final固定

       写法规范:
        常量所有字母都大写,多个单词,中间用下划线连接

Guess you like

Origin blog.csdn.net/weixin_42947972/article/details/102472074