国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

在Java中如何模擬多繼承

2019-11-18 13:37:04
字體:
供稿:網(wǎng)友

  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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 通化市| 崇明县| 日照市| 隆德县| 甘泉县| 龙口市| 阆中市| 喀喇沁旗| 龙泉市| 岳池县| 榆社县| 双流县| 明星| 延吉市| 马龙县| 紫金县| 同心县| 云安县| 泉州市| 永宁县| 平利县| 民权县| 唐海县| 湖北省| 乌兰县| 正阳县| 灌阳县| 泽普县| 台安县| 缙云县| 广德县| 田林县| 浪卡子县| 沅陵县| 禹城市| 巴里| 商城县| 嘉鱼县| 茂名市| 洛南县| 资中县|