zoukankan      html  css  js  c++  java
  • java学习笔记 final 关键字

    final在java中表示最终,也可称为完结器。可以使用final关键字声明类、属性、方法。

     1 public class FinalTest {
     2     public static void main(String[] args) {
     3         Cat c = new Cat("猫类",9527);
     4         c.scream();    
     5         c.m(3);
     6     }    
     7 }
     8 
     9 //final class Animal {//return:无法从最终Animal进行继承
    10 class Animal {
    11  //final String name = "动物类";
    12  String name = "动物类";
    13  
    14  Animal(String a) {
    15      this.name = a;    //无法为最终变量name分配值
    16  }
    17  
    18  //public final void scream() {//return:Cat中的scream()无法覆盖Animal中的scream()被覆盖的方法为final
    19  public void scream() {
    20      System.out.println("animal is scream");    
    21  }    
    22 }
    23 
    24 class Cat extends Animal {
    25     int id;    
    26     double d = Long.MAX_VALUE;
    27     
    28     Cat(String a,int id) {
    29         super(a);
    30         this.id = id;    
    31          
    32     }
    33     
    34     public void scream() {
    35      System.out.println("Animal is scream");    
    36      System.out.println(d);
    37  }
    38  
    39  public void m(final int c) {
    40      System.out.println(c);
    41      //c = 5;//return:错误 不能分配最终参数c。    
    42  }
    43 }
    44 
    45 /*
    46 1.final声明的类为最终类,不能被继承。
    47 2.final声明的方法为最终方法,不能被重写覆盖。
    48 3.final声明的变量即成为常亮,常量不可以修改。(在使用final声明变量时,要求全部的字母大写,如Long.MAX_VALUE)
    49 4.如果把一个方法的形参声明为final类型的,那么只有在调用这个方法时付给形参以实际的值,在方法里不能再改变行参的值了。
    50 
    51 */
  • 相关阅读:
    博客园如何运行代码
    视觉差
    h5 播放器 -3
    播放器 视频 音频 -1
    游戏 保卫萝卜
    跟踪算法
    走口字

    联动日历
    jq 抽奖
  • 原文地址:https://www.cnblogs.com/yhwsy/p/5710798.html
Copyright © 2011-2022 走看看