final关键字

/*
 * 为什么需要学习final关键字
 * 	修饰变量成为自定义常量,可以提高程序的可读性,同时不能够被二次赋值
 * 	常量的分类:
 * 		字面值常量: 2 2.5 false null "abc"
 * 		自定义常量: public static final int NUM = 100;
 * final: 最终的, 最后的
 * 
 * 	final能够修饰变量 方法 类
 *  final修饰变量 只能够赋值一次,表示常量, 可以提高程序的可读性
 *  	成员变量: 必须给出默认值,后期系统会赋值默认值
 *  	局部变量: 没有默认值,可以后面自己给默认值
 *  
 *  final 修饰方法 不能够被子类重写
 *  
 *  final 修饰类 不能够被继承
 * 	
 */
public class FinalDemo01 {
	public static void main(String[] args) {
		// Game.START = 20;
		int state = 0;
		switch (state) {
		case Game.START:
			System.out.println("游戏开始");
			break;
		case Game.END:
			System.out.println("游戏结束");
			break;
		case Game.RUNNING:
			System.out.println("游戏运行中");
			break;
		default:
			break;
		}
	}
}

final class A {
	public final int NUM = 200;
	
	public void show() {
//		NUM = 200;
		final int I;
		I = 200;
		// I = 300;
	}
	
	public final void test() {
		
	}
}

class B /*extends A*/ {
	/*public final void test() {
		
	}*/
}

class Game {
	public static final int START = 0;
	public static final int END = 1;
	public static final int RUNNING = 2;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值