• java final 关键词


    package day9;
    
    /**
     * Created by admin on 2018/11/17.
     * final可以修饰类,方法,变量
    
         特点:
         final可以修饰类,该类不能被继承。
         final可以修饰方法,该方法不能被重写。(覆盖,复写)
         final可以修饰变量,该变量不能被重新赋值。因为这个变量其实常量。
    
         常量:
            A:字面值常量
                "hello",10,true
            B:自定义常量
                final int x = 10;
         final修饰变量的初始化时机
             A:被final修饰的变量只能赋值一次。
             B:在构造方法完毕前。(非静态的常量)
     */
    public class FinalDemo {
        public static void main(String[] args) {
            Zi2 z = new Zi2();
            z.show();
            Demo d = new Demo();
            System.out.println(d.num + d.num3);
        }
    }
    
    class Fu2{
        public int num = 10;
        public final int num2 = 20;
    
    }
    class Zi2 extends Fu2 {
    
        public void show(){
            num = 100;
            System.out.println(num);
            //无法为最终变量num2 分配值
            //num2 = 200;
            System.out.println(num2);
        }
    }
    class Demo {
        int num;
        final int num3;
        public Demo() {
            num = 100;
            num3 = 300;
        }
    
    }
    

      

  • 相关阅读:
    css color
    css 常用单位
    CSS grid layout
    C++ vector 容器
    我了解到的 JQuery 的定时器
    ORACLE 中如何截取到时间的年月日中的年
    复选框的 全选 反选 全不选
    模糊查询 字符串 多选查询
    JAVA对象JSON数据互相转换
    显示 / 隐藏 <a> 标签
  • 原文地址:https://www.cnblogs.com/royfans/p/9974182.html
Copyright © 2020-2023  润新知