首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【说站】java动态绑定怎么用

【说站】java动态绑定怎么用

作者头像
很酷的站长
发布于 2022-11-23 15:36:59
发布于 2022-11-23 15:36:59
1.6K0
举报

java动态绑定怎么用

用法

1、程序在编译的时候调用的其实是父类的eat方法,但是在运行时运行的则是子类的eat方法,运行期间发生了绑定。

2、使用前题,先向上转型,通过父类引用来调用父类和子类同名的覆盖方法

实例

代码语言:javascript
复制
package chapeter04;
 
class Test
{
public Test() { }
public void setName(String n)
{
this.name=n;
System.out.println("在父类中");
}
public String getName()
{
return this.name;
}
private String name;
}
 
public class Sample4_12 extends Test
{
public void setArea(String a)
{
this.area=a;
}
 
public String getArea()
{
return this.area;
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Sample4_12 child = new Sample4_12();
Test test []=new Test[2];
test[0]=child;
test[0].setName("silence");
test[1]=new Test();
}
private String area;
 
}

以上就是java动态绑定的使用,大家在开始使用前,需要对其的使用前提进行明确。更多Java学习指路:Java基础

本教程操作环境:windows7系统、java10版,DELL G3电脑。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • java动态绑定怎么用
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档