java中#都是什么用处_binding在Java中得具体作用?是什么意思?有什么作用?

news/2024/7/1 20:40:44

在Java中,父类与子类之间的类型转换有两种,一种是子类向上转型父类(这永远是安全的),另一种是父类向下转型为子类(这得由相应的instanceof来判断数据类型,同时这也是一种不安全的转换)Base class 与derived class 之间,为了达到从一个特定类型转为一个通用类型(有点多态的味道),可以借助向上]转型这一工具(即子类向上转为父类类型的),因为Java里的对象引用都是dynamic binding或者lated bindin或者runing time binding,缺点就是在向上转型的过程中会丢失了属于子类专用的方法;向下转型中用得最多的就是interface的运用。

下面用一段代码讲示: class Base{ public void baseMethod() {  System。out。println("into Base#baseMethod");

}

public void fuc() {   System。

out。println("into Base#fuc");

} }class Child extends Base{ public void childMethod() {  System。out。println("into Child#childMethod");

}

public void fuc() {  System。

out。println("into Child#fuc");   } }class CCChild extends Child{ public void ccchildMethod() {  System。

out。println("into CCChild#ccchildMethod");

}

public void fuc() {  System。out。println("into CCChild#fuc");   } }class DChild extends CCChild{ public void dchildMethod() {  System。

out。println("into DChild#dchildMethod");

}

public void fuc() {  System。out。println("into DChild#fuc");   } }

public class DynamicBindingTest{ public static void main(String[]args) {  Base bs=new Child();  bs。

fuc();//dynamic binding derived Child so call Child#fuc  // bs。childMethod();//error:this method has been lost when upcasting to base class Base

Base cbs=new CCChild();  cbs。

fuc();  // bs。ccchildMethod();//error:this method has been lost when upcasting to base class Base

System。out。

println("=================");  Child c=new Child();  CCChild ccc=new CCChild();  DChild dc=new DChild();  testCall(c);  testCall(ccc);  testCall(dc); }

static void testCall(Base b) {  b。

fuc(); } }控制台打印出来的结果into Child#fucinto CCChild#fuc================into Child#fucinto CCChild#fucinto DChild#fuc。

全部


http://www.niftyadmin.cn/n/3542243.html

相关文章

MyBatis整体Review

一,整体结构 mybatis是一个持久层的框架,是apache下的顶级项目。 mybatis托管到goolecode下,再后来托管到github下(https://github.com/mybatis/mybatis-3/releases)。 中文手册:http://www.mybatis.org/mybatis-3/zh/index.html …

批量建 日期文件夹_EXCEL一篇搞定,根据当天日期批量创建数百个文件夹

我是做销售统计的,每天都需要收集门店的日报,每天都需要针对每个门店创建一个文件夹第一层是日期,然后是门店名称对应的文件夹我每天都要一个一个创建,现在门店多了,创建都花了半天时间,有简单的方法吗&…

【转载】ArcGIS中topolopy说明

【本文转载于:http://blog.csdn.net/lc80/archive/2007/01/14/1482692.aspx,版权属于原创作者】 [第一部分]Arcgis中topolopy说明: 在arcgis中有关topolopy操作,,有两个地方,一个是在arccatalog中&#xff…

一段html代码引发的页面重复请求问题

<meta http-equiv"Refresh" content"0; url"> 这个页面本来是说每隔0秒刷新一下页面&#xff0c;并跳转到url所指定的页面去&#xff0c;其实就相当于页面跳转。但是实际是由于url为空&#xff0c;这样就不停的请求这张页面&#xff0c;导致页面狂…

python 单元测试_聊聊 Python 的单元测试框架(二):nose 和它的继任者 nose2

? “Python猫” &#xff0c;一个值得加星标的公众号剧照 | 《神雕侠侣》作者&#xff1a;HelloGitHub-Prodesire出处&#xff1a;HelloGitHub文中涉及的示例代码&#xff0c;已同步更新到 HelloGitHub-Team 仓库点击本文最下方的“阅读原文”即可获取一、nose nose[1] 是一个…

benet s2 试卷

benet s2 试卷转载于:https://blog.51cto.com/hzwsz/63637

锁源码分析-AQS实现

2019独角兽企业重金招聘Python工程师标准>>> 1 目录 2 Lock和Condition接口 最近打算将jdk的Lock系列详细的分析一下&#xff0c;解决以下几个问题&#xff1a; AQS详细分析独占锁、共享锁、读写锁是否可重入公平锁和非公平锁的区别&#xff0c;这里的公平和非公平是…

memcached原理应用及问题

背景&#xff1a; 在做服务器端应用开发&#xff0c;我们常常要考虑很多东西。从单台机器到集群&#xff0c;从混乱的结构到分层结构&#xff0c;接着是系统优化&#xff0c;中间作cache处理&#xff0c;后端优化缓存。 今天就讨论一下服务器端缓存&#xff1a;&#xff09; …