final修饰变量
注意:
-
由final修饰的变量一旦赋了初值,就不能被重新赋值---只能被赋一次值
final int MAX_SPEED = 120;
final修饰方法
注意:
-
该方法不可被子类重写,但是可以被重载
final void study(){}
实例:
class Animal{
//叫一声的方法
public final void shout(){
System.out.println("叫了一声!");
}
}
//第二个类,狗
class Dog extends Animal{
//重写方法
public void shout(){
System.out.println("汪汪汪");
}
/*
上面的方法加了final下面就会提示编译错误
被final修饰的方法不能被重写了
*/
final修饰类
注意:
-
修饰的类不能被继承。比如:Math、String等
fianl class A{}
实例:
final class Animal{
//叫一声的方法
public final void shout(){
System.out.println("叫了一声!");
}
}
//第二个类,狗
class Dog extends Animal{
//重写方法
public void shout(){
System.out.println("汪汪汪");
}
/*
上面的方法加了final下面就会提示编译错误
被final修饰的方法不能被重写了
*/