首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

步骤以及成员变量的动态绑定

2012-11-07 
方法以及成员变量的动态绑定在运行过程中,成员变量(包括静态变量和实例变量)以及静态方法都和引用变量的声

方法以及成员变量的动态绑定

在运行过程中,成员变量(包括静态变量和实例变量)以及静态方法都和引用变量的声明类型绑定, 实例方法将和实例绑定.举例如下:

class Father{

private int private_var;
static int static_var;
public int public_var;

private void private_method(){}
static void static_method(){}
public void public_method(){}

}

class Son extends Father{

private int private_var;
static int static_var;
public int public_var;

private void private_method(){}
static void static_method(){}

}

以上代码中,子类Son和父类Father具有同名的变量和方法.对于以下代码,引用变量f声明为Father类型,实际引用的是Son的实例,那么通过变量f来访问成员变量和方法,绑定关系如下:

Father f=new Son();

int v1=f.private_var; //bind with father's private_var
int v2=f.static_var; //bind with father's static_var
int v3=f.public_var; //bind with father's public_var

f.private_method(); //bind with father's private_method,私有的
f.static_method(); //bind with father's static_method
f.public_method(); //bind with son's public_method,继承父类的

热点排行