final:最终的,不可改变的-----------单独应用几率低
修饰变量:变量不能被改变
//演示final修饰变量 class Aoo{ final int num = 5; void show(){ //num = 55; //编译错误,final的变量不能被改变 } }
修饰方法:方法不能被重写
//演示final修饰方法 class Boo{ final void show(){} } class Coo extends Boo{ //void show(){} //编译错误,final修饰的方法不能被重写 //演示final修饰类 final class Doo{} //class Eoo extends Doo{} //编译错误,final的类不能被继承 class Foo{} final class Goo extends Foo{} //不能当老爸,但能当儿子
static final常量:应用率高
public class StaticFinalDemo { public static void main(String[] args) { System.out.println(Hoo.PI); //通过类名点来访问 //Hoo.PI = 3.1415926; //编译错误,常量不能被改变 //1)加载Ioo.class到方法区中 //2)将静态变量num一并存储到方法区中 //3)到方法区中获取num的值并输出 System.out.println(Ioo.num); //编译器在编译时将常量直接替换为具体的值,效率高 //相当于System.out.println(5); System.out.println(Ioo.COUNT); } } class Ioo{ public static int num = 5; //静态变量 public static final int COUNT = 5; //常量 } class Hoo{ public static final double PI = 3.14159; //public static final int NUM; //编译错误,常量必须声明同时初始化 }
抽象方法:
抽象类:
由abstract修饰
包含抽象方法的类必须是抽象类
抽象类不能被实例化(new对象)
抽象类是需要被继承的,派生类:
抽象类的意义:
封装共有的属性和行为--------------------代码复用
为所有派生类提供统一的类型-----------向上造型---代码复用
可以包含抽象方法,为所有派生类提供统一的入口(能点出来)
派生类的行为不同,但入口是一致的,同时相当于定义了一个标准