final关键字是最终的意思,可以修饰成员方法,成员变量,类
final修饰的特点
修饰方法:表明该方法是最终方法,不能被重写
修饰变量:表明该变量是常量,不能再次被赋值
修饰类:表明该类是最终类,不能被继承
package com.ithema_31;
/*
public class Fu {
public final void method(){
System.out.println("Fu method");
}
}
*/
public final class Fu {
public final void method(){
System.out.println("Fu method");
}
}
package com.ithema_31;
public class Zi extends F u{
public final int age = 20;
public void show(){
// age = 100;
System.out.println(age);
}
// @Override
// public void method(){
// System.out.println("Zi method");
// }
}
package com.ithema_31;
/*
测试类
*/
public class Demo {
public static void main(String[] args) {
Zi z = new Zi();
z.method();
z.show();
}
}