首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 认证考试-> ORACLE认证
在Java中如何模拟多继承-JSP教程,Java技巧及代码
作者:网友供稿 点击:254
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
simulating multiple inheritance in java
article author: mike van atter
from book: codenotes for java
date published: february 1, 2002
purpose of multiple inheritance
multiple inheritance allows a single class to extend two parent classes and thus provide the methods of both parent classes. unlike c++, java does not explicitly support multiple inheritance, allowing a class to extend only a single parent class. however, as we will show in this article, it is possible to simulate multiple inheritance, allowing a single class to provide the methods, and the respective implementations, of two parent classes. the strategy that is introduced in this article is also easily extendible to provide inheritance of three or more parent classes.

simulating multiple inheritance
in this article we will use a simple example to demonstrate how to simulate multiple inheritance in java. we will begin with the nextodd and nexteven classes, shown in listing 1.1 and listing 1.2 respectively. we will then create a new class, which we will call evenodd, that provides the functionality of both classes.


// repeated calls to the getnextodd method will
return the next
// odd number (i.e. the first call will return 1, the second
// call 3, etc.
public class nextodd {
// the last odd number returned by the getnextodd method
private int lastodd = -1;

public nextodd() {
this.lastodd = -1;
} // nextodd

// selects a different starting point for the odd numbers
// ensures that the starting point is in fact an odd number
public nextodd(int start) {
this.lastodd = ((int)start/2) * 2 + 1;
} // nextodd

// retrieves the next odd number
public int getnextodd() {
return lastodd += 2;
} // getnext
} // nextodd




listing 1.1: nextodd.java


// repeated calls to the getnexteven method will
return the
// next even number (i.e the first call will return 0, the
// second call will return 2, etc.)
public class nexteven {
// the last even number returned by getnexteven
private int lasteven = -2;

public nexteven() {
this.lasteven = -2;
} // nexteven

// selects a different starting point for the even numbers
// ensures that the starting point is in fact an even #
public nexteven(int start) {
this.lasteven = ((int)(start/2)) * 2;
} // constructor

// retrieves the next even number
public int getnexteven() {
return lasteven +=2;
} // getnexteven

} // nexteven




listing 1.2: nexteven.java

as java only allows for extending a single class through the extends keyword, we will have to provide another manner for extending more than one class. in this example, we will extend the nexteven class by using the extends keyword and use a new interface, which we will call oddinterface, and an implementation of the new interface, which we will call oddchild, to extend the nextodd class.

the first step in extending the nextodd class is to define an interface with the same methods as the nextodd class, as shown in listing 1.3. notice that the parameters, function names, and return values for all methods in the interface must be the same as the original class.


public interface oddinterface {
public int getnextodd();
} // oddinterface




listing 1.3: oddinterface.java

once we have created oddinterface, the next step is to create an implementation of oddinterface that also extends the nextodd class, as shown in listing 1.4. by extending the nextodd class, which, as previously explained, has all the same method prototypes as oddinterface, we do not have to implement any of the methods in oddinterface and only have to provide constructors for the new class, which we will call oddchild. these constructors simply call the constructors of the nextodd class using the super() method. the oddchild class now provides the exact implementation of all methods of the nextodd class, without the developer having to know anything about the way in which nextodd was originally implemented.


public class oddchild extends nextodd implements
oddinterface {
public oddchild() {
super();
} // constructor

public oddchild(int start) {
super(start);
} // constructor

} // oddchild




listing 1.4: oddchild.java

with our implementation of the oddinterface class, we can now create a class that will extend both the nexteven class and the nextodd class. this new class will be called evenodd and is shown in listing 1.5. because java allows you to extend only a single class, evenodd will extend the nexteven class and use oddinterface and oddchild to extend the nextodd class.

in order to be able to call the evenodd.getnextodd() method, evenodd will implement oddinterface because oddinterface has all the same method prototypes as nextodd. this means that we also must provide an implementation of all the oddinterface methods, and as a result all the nextodd methods, within evenodd. to ensure these methods have the same implementation as the nextodd methods, we will create a private instance of the oddchild class, which we will call oddgenerator, and call the respective oddgenerator method. for example, in the evenodd.getnextodd() method, we call oddgenerator.getnextodd(). the evenodd class now provides the same functionality and implementation of both the nextodd and nexteven classes.


public class evenodd extends nexteven implements
oddinterface {
public evenodd() {
super();
oddgenerator = new oddchild();
} // evenodd

// initializes the starting point of both the odd # generator
// and the even # generator
public evenodd(int oddstart, int evenstart) {
super(evenstart);
oddgenerator = new oddchild(oddstart);
} // evenodd

public int getnextodd() {
return oddgenerator.getnextodd();
} // getnextodd

private final oddinterface oddgenerator;
} // evenodd




listing 1.5: evenodd.java

unfortunately, because java does only allow you to extend a single class, you will only be able to cast the evenodd class to a nexteven class and not to a nextodd class as you would be able to if multiple inheritance were directly supported by java. if you wish to be able to cast an evenodd object to a nextodd class, you will have to provide a method for extracting an instance of the nextodd class similar to the getnextoddobj() method in listing 1.6.


public nextodd getnextoddobj() {
return (nextodd)oddgenerator;
} // getnextodd




listing 1.6: returning a nextodd instance

in fact, this multiple inheritance limitation is often avoided by creating a factory class with many methods similar to listing 1.6.

summary

create an interface with all the same method prototypes as the base class you will be extending.
create a class that implements the interface created in step 1 and extends the base class.
in the child class, implement the interface created in step 1 and create a private instance of the class defined in step 2. in all the methods defined in the interface, simply call the corresponding method in the class created in step 2.


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·一个特牛的日期时间判断正则表达式-ASP教程,正则表达式
·Remoting编程知识一-.NET教程,.NET Framework
·从客户端检测到有潜在危险的Request.Form 值-ASP教程,客户端相关
·关于DataRow和DataColumn的一点个人简单理解-.NET教程,数据库应用
·DataTable.Select使用小心得-.NET教程,数据库应用
·在Java中如何模拟多继承-JSP教程,Java技巧及代码
·上海移动数据网综合网管的实现
·在VC中用GDI+绘制角度可变的颜色渐变效果-.NET教程,VB.Net语言
·超漂亮的绿色按扭制做-网页设计,Photoshop
·Java获取Html变量的值-JSP教程,Java技巧及代码
最新文章
·阿里联盟:关于结算时间问题的统一说明_网赚技巧
·谷歌官方组织adsense优化大赛_网赚技巧
·google adsense 2007巡讲大会上海站总结_网赚技巧
·网络只是一个开始:专访91now站长小鱼头_站长访谈
·asp.net应用程序资源访问安全模型_asp.net技巧
·给那些迷惑于做垃圾站的站长们_站长心得
·免费——不是威客网站的杀手锏_站长心得
·圈圈浅谈个人网站发展和赚钱的模式-网站推广的口碑篇_站长心得
·说网解络之web2.0概念诠释(1)_站长心得
·google adsense系列技巧100条_google推广
相关主题
  • 在javascript中使用正则表达式_javascript教程
  • 在javascript中实现命名空间_javascript教程
  • 在java和.net平台的加密术比较_asp.net技巧
  • 在java中读写excel文件_java认证
  • 在java中使用正则表达式_java认证
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭