final
final关键字的作用:
修饰变量:一旦给变量赋值不能再更改
final int MAX=120;
修饰方法:继承时不能重写,只能重载
class Animal {
final void shout() {
System.out.println("叫了一声");
}
}
class Dog extends Animal {
void shout() { //报错
System.out.println("叫了一声");
}
}
class Cat extends Animal {
void shout() { //报错
System.out.println("喵喵");
}
}
修饰类:不能继承
final class Animal {
void shout() {
System.out.println("叫了一声");
}
}
class Dog extends Animal { //报错
void shout() {
System.out.println("叫了一声");
}
}
class Cat extends Animal { //报错
void shout() {
System.out.println("喵喵");
}
}