
用法
1、程序在编译的时候调用的其实是父类的eat方法,但是在运行时运行的则是子类的eat方法,运行期间发生了绑定。
2、使用前题,先向上转型,通过父类引用来调用父类和子类同名的覆盖方法
实例
packagechapeter04;
classTest
{
publicTest(){}
publicvoidsetName(Stringn)
{
this.name=n;
System.out.println("在父类中");
}
publicStringgetName()
{
returnthis.name;
}
privateStringname;
}
publicclassSample4_12extendsTest
{
publicvoidsetArea(Stringa)
{
this.area=a;
}
publicStringgetArea()
{
returnthis.area;
}
publicstaticvoidmain(String[]args)
{
//TODOAuto-generatedmethodstub
Sample4_12child=newSample4_12();
Testtest[]=newTest[2];
test[0]=child;
test[0].setName("silence");
test[1]=newTest();
}
privateStringarea;
}
以上就是java动态绑定的使用,大家在开始使用前,需要对其的使用前提进行明确。更多Java学习指路:Java基础
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


















































暂无评论内容