在我前面的一篇文章中,有写到C++的静态绑定和动态绑定.期间遇到了很多问题.
而用Java的话,可以很轻松的实现所要的需求.
但是...
/**
* @time 上午10:23:34
* @fileName TestStaticDynamicBinding.java
* @package me.banxi.oo
*/
package me.banxi.oo;
class Figure{
public static final String name = "Figure";
public Figure(){
}
public double area(){
return 0.0;
}
}
class Circle extends Figure{
public static final String name = "Circle";
private double _radius;
public Circle() {
}
public Circle(double radius){
_radius = radius;
}
public double area(){
return Math.PI*_radius*_radius;
}
}
class Rectangle extends Figure{
public static final String name = "Rectangle";
private double _length;
private double _width;
public Rectangle(){
}
public Rectangle(double length,double width){
_length = length;
_width = width;
}
public double area(){
return _length*_width;
}
}
/**
* @author banxi1988
*/
public class TestStaticDynamicBinding {
static void printArea(Figure figure){
System.out.println(figure.name+"的面积是:"+figure.area());
}
public static void main(String[] args) {
Figure figure = new Figure();
printArea(figure);
Circle circle = new Circle(2.0);
printArea(circle);
Rectangle rect = new Rectangle(3.0,4.0);
printArea(rect);
}
}
/**
*
banxi1988@banxi:~/cppworkspace/C++_pk_Java/bin$ java me.banxi.oo.TestStaticDynamicBinding
Figure的面积是:0.0
Figure的面积是:12.566370614359172
Figure的面积是:12.0
banxi1988@banxi:~/cppworkspace/C++_pk_Java/bin$
***/
由此可见Java中类成员方法都是动态绑定的.
但是类中静态成员还是静态绑定的.
经过测试
发现,对于类的中字段属性,也是静态绑定的..
所以就算我把name声明改成下面的,最后结果输出的还是Figure.
public final String name = "Circle";
修改后如下,完成,静态绑定:
/**
* @time 上午10:23:34
* @fileName TestStaticDynamicBinding.java
* @package me.banxi.oo
*/
package me.banxi.oo;
class Figure{
private static final String name = "Figure";
public Figure(){
}
public String getName(){
return name;
}
public double area(){
return 0.0;
}
}
class Circle extends Figure{
private static final String name = "Circle";
private double _radius;
public Circle() {
}
public Circle(double radius){
_radius = radius;
}
public String getName(){
return name;
}
public double area(){
return Math.PI*_radius*_radius;
}
}
class Rectangle extends Figure{
private static final String name = "Rectangle";
private double _length;
private double _width;
public Rectangle(){
}
public Rectangle(double length,double width){
_length = length;
_width = width;
}
public String getName(){
return name;
}
public double area(){
return _length*_width;
}
}
/**
* @author banxi1988
*/
public class TestStaticDynamicBinding {
static void printArea(Figure figure){
System.out.println(figure.getName()+"的面积是:"+figure.area());
}
public static void main(String[] args) {
Figure figure = new Figure();
printArea(figure);
Circle circle = new Circle(2.0);
printArea(circle);
Rectangle rect = new Rectangle(3.0,4.0);
printArea(rect);
}
}
/**
*
banxi1988@banxi:~/cppworkspace/C++_pk_Java/bin$ java me.banxi.oo.TestStaticDynamicBinding
Figure的面积是:0.0
Circle的面积是:12.566370614359172
Rectangle的面积是:12.0
banxi1988@banxi:~/cppworkspace/C++_pk_Java/bin$
***/
下面测试一下Java中同类中
构造函数的调用.
如下,将Rectangle类的name属性.写成下面的形式:
private static final String name;//error
private double _length;
private double _width;
public Rectangle(){
name = "Rectangle";
}
public Rectangle(double length,double width){
this();
_length = length;
_width = width;
}
public String getName(){
return name;
}
上面有一句语句是有错的,就是static final同时修改的字段.name没有直接初始化.
就违反了Java中上述两个修饰的属性了.
支持任意一个都可以.
修改后如下:
class Rectangle extends Figure{
private static String name;
private double _length;
private double _width;
public Rectangle(){
name = "Rectangle";
}
public Rectangle(double length,double width){
this();
_length = length;
_width = width;
}
public String getName(){
return name;
}
public double area(){
return _length*_width;
}
}
总结Java中的方法是动态绑定的.而字段则是静态绑定.
在Java中可以直接对字段,进行初始化.
而C++中则静态属性字段要在类外部初始化.实例属性字段要在构造函数中初始化.
而且在C++中调用同一类中构造函数好像没有java中的this()机制.
如果在C++中直接调用另一个构造函数的话,生成了另一个隐藏的对象.