remexer(remexer)的说法 <<final 类表示不能被继承;方法和数据表示不能被改变。>> 不确切。 声明为final的方法是不能派生类被覆盖的。所以在template method中通常都定义为final。 而声明为final的变量并不是不能改变,而是引用的地址不会变改变,引用的内容是可以改变的。 比如这样完全是合法的: << final Data date = new Date(1); date.setDate(2); >>
修正上面的一个BUG: << date.setTime(2); >> 关于final的具体含义,请参见JLS §4.5.4 final Variables: << A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.A blank final is a final variable whose declaration lacks an initializer. Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object. This applies also to arrays, because arrays are objects; if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array. >>
是不是类常量要用final static 而方法常量一般使用final啊
static是指无论此类有多少个实例,它定义的东西都只有一个,它不依赖于实例而存在
final 定义的类是指不能有子类的类 final 定义的方法不能在子类中被覆盖static型是属于类本身的,而不是为该类的每一个实例对象产生一个单独的拷贝 同样,static方法也是属于整个类而不是属于某个对象的。 如: 类class1 中有staitc field x 和static 型方法y(),则它们可以由class1.x 和class.y()所存取,而独立于任何的对象实例一个最好的例子就是Java.lang.Math类,它说明了static方法是如何使用的 去看看API吧
final 作用:方法、数据、类。
static 表示不用实例就可以用类名+..来引用;
final 类表示不能被继承;方法和数据表示不能被改变。
够详细吧,呵呵。
白写了。算了。remexer(remexer)说的差不多共用的东西,可以用static的。
<<final 类表示不能被继承;方法和数据表示不能被改变。>>
不确切。
声明为final的方法是不能派生类被覆盖的。所以在template method中通常都定义为final。
而声明为final的变量并不是不能改变,而是引用的地址不会变改变,引用的内容是可以改变的。
比如这样完全是合法的:
<<
final Data date = new Date(1);
date.setDate(2);
>>
<<
date.setTime(2);
>>
关于final的具体含义,请参见JLS §4.5.4 final Variables:
<<
A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.A blank final is a final variable whose declaration lacks an initializer. Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object. This applies also to arrays, because arrays are objects; if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.
>>
而方法常量一般使用final啊
final 定义的方法不能在子类中被覆盖static型是属于类本身的,而不是为该类的每一个实例对象产生一个单独的拷贝
同样,static方法也是属于整个类而不是属于某个对象的。
如: 类class1 中有staitc field x 和static 型方法y(),则它们可以由class1.x
和class.y()所存取,而独立于任何的对象实例一个最好的例子就是Java.lang.Math类,它说明了static方法是如何使用的
去看看API吧