到目前分享的文章,我们会发现,我们已经用到了只含有一个超类和一个子类的简单类层次结构。然而,你可以如你所愿的建立包含任意多层继承的类层次。

前面提到,用一个子类作为另一个类的超类是完全可以接受的。例如,给定三个类A,B和C。C是B的一个子类,而B又是A的一个子类。当这种类型的情形发生时,每个子类继承它的所有超类的属性。

这种情况下,C继承B和A的所有方面。为了理解多级层次的用途,考虑下面的程序。该程序中,子类BoxWeight用作超类来创建一个名为Shipment的子类。Shipment继承了BoxWeight和Box的所有特征,并且增加了一个名为cost的成员,该成员记录了运送这样一个小包的费用。

// Extend BoxWeight to include shipping costs.
// Start with Box.
class Box { private double width; private double height; private double depth; // construct clone of an object Box(Box ob) { // pass object to constructor width = ob.width; height = ob.height; depth = ob.depth; } // constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; } // constructor used when no dimensions specified Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box } // constructor used when cube is created Box(double len) { width = height = depth = len; } // compute and return volume double volume() { return width * height * depth; }
}
// Add weight.
class BoxWeight extends Box { double weight; // weight of box // construct clone of an object BoxWeight(BoxWeight ob) { // pass object to constructor super(ob); weight = ob.weight; } // constructor when all parameters are specified BoxWeight(double w, double h, double d, double m) { super(w, h, d); // call superclass constructor weight = m; } // default constructor BoxWeight() { super(); weight = -1; } // constructor used when cube is created BoxWeight(double len, double m) { super(len); weight = m; }
}
// Add shipping costs
class Shipment extends BoxWeight { double cost; // construct clone of an object Shipment(Shipment ob) { // pass object to constructor super(ob); cost = ob.cost; } // constructor when all parameters are specified Shipment(double w, double h, double d, double m, double c) { super(w, h, d, m); // call superclass constructor cost = c; } // default constructor Shipment() { super(); cost = -1; } // constructor used when cube is created Shipment(double len, double m, double c) { super(len, m); cost = c; }
}
class DemoShipment { public static void main(String args[]) { Shipment shipment1 = new Shipment(10, 20, 15, 10, 3.41); Shipment shipment2 = new Shipment(2, 3, 4, 0.76, 1.28); double vol; vol = shipment1.volume(); System.out.println("Volume of shipment1 is " + vol); System.out.println("Weight of shipment1 is " + shipment1.weight); System.out.println("Shipping cost: $" + shipment1.cost); System.out.println(); vol = shipment2.volume(); System.out.println("Volume of shipment2 is " + vol); System.out.println("Weight of shipment2 is " + shipment2.weight); System.out.println("Shipping cost: $" + shipment2.cost); }
}

下面是该程序的输出:

Volume of shipment1 is 3000.0
Weight of shipment1 is 10.0
Shipping cost: $3.41
Volume of shipment2 is 24.0
Weight of shipment2 is 0.76
Shipping cost: $1.28

因为继承关系,Shipment可以利用原先定义好的Box和BoxWeight类,仅为自己增加特殊用途的其他信息。这体现了继承的部分价值;它允许代码重用。

该例阐述了另一个重要的知识点:super( )总是引用子类最接近的超类的构造函数。Shipment中super( )调用了BoxWeight的构造函数。BoxWeight中的super( )调用了Box中的构造函数。在类层次结构中,如果超类构造函数需要参数,那么不论子类它自己需不需要参数,所有子类必须向上传递这些参数。

Java中所有三个类可以被放置在它们自己的文件中且可以独立编译。实际上,在创建类层次结构的时候,使用分离的文件是常见的,不是罕见的。

开课吧课堂之如何创建多级类层次相关推荐

  1. 开课吧课堂:什么是HashSet类

    HashSet扩展AbstractSet并且实现Set接口.它创建一个类集,该类集使用散列表进行存储.正像大多数读者很可能知道的那样,散列表通过使用称之为散列法的机制来存储信息. 在散列(hashin ...

  2. 开课吧课堂之如何创建自己的异常子类

    尽管Java的内置异常处理大多数常见错误,你也许希望建立你自己的异常类型来处理你所应用的特殊情况.这是非常简单的:只要定义Exception的一个子类就可以了(Exception当然是Throwabl ...

  3. Java创建多级文件夹

    Java创建多级文件夹 Java的File类坑巨多,就创建文件来说,多级创建和单级创建竟然不一样. 直接上API mkdir只能创建一个文件夹,而想创建多级文件夹需要用mkdirs,多了个s. 来来, ...

  4. Java基础--创建多级文件夹

    Java创建多级文件夹 /** File类中的mkdir()和mkdirs(): mkdir():只能创建一层目录. mkdirs():可以创建多层目录 */ //代码:path可以是//路径 Str ...

  5. SpringBoot 框架中 使用Spring Aop 、创建注解、创建枚举类 使用过程记录

    1.开始 在Springboot框架中引入AOP <dependency><groupId>org.springframework.boot</groupId>&l ...

  6. 计算机公开课课前互动小游戏,公开课前课堂小游戏

    引导语:一年级拼音教学小游戏玩是孩子的天性,游戏自然就会成为一年级拼音教学的常客!下面是yjbys小编收集了一些关于公开课前拼音课堂小游戏,希望对你有帮助. 篇一:公开课前课堂小游戏 适用范围: 用于 ...

  7. php创建多级目录完整封装类操作

    创建多级目录函数中调用创建指定下的指定文件的函数: public function create_dir($dir,$mode=0777){return is_dir($dir) or ($this- ...

  8. 使用tolua++编译pkg,从而创建自定义类让Lua脚本使用

    2019独角兽企业重金招聘Python工程师标准>>> 在Lua第三篇中介绍了,如何在cocos2dx中使用Lua创建自定义类供Lua脚本调用使用,当时出于Himi对Lua研究不够深 ...

  9. python动态创建类_Python中通过参数动态创建扩展类(class)

    class Bar: def super_cool_function(self): print("Cool") 1.利用Python闭包动态扩展类 通过在内部创建并从函数返回它来动 ...

最新文章

  1. Nmap安装和扫描(一:Nmap安装和扫描基础知识点总结)
  2. Java虚拟机(JVM)
  3. 现成Android 5.0系统源代码
  4. 职称计算机考试word2003真题,职称计算机考试《Word2003》历年真题回顾(4)
  5. 多线程之HttpClient
  6. 【Python基础】盘点 Python 10 大常用数据结构(下篇)
  7. Java Web学习(三)数据加密方式详解
  8. Windows下MySQL数据库名及表名无法大写的问题
  9. 一个十年SAP CRM老司机对产品主数据的理解
  10. 使用 WRK 压力测试工具对 ASP.NET Core 的接口进行压力测试
  11. linux之lsusb命令和cd -命令使用总结
  12. leetcode 49. 字母异位词分组(排序+hash)
  13. League of Legends 通过 游戏ID查询玩家QQ号码。
  14. Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version)(单调栈)
  15. channel使用法则
  16. 【macOS游戏】Cities:天际线
  17. 【计算机网络】TCP/IP协议(DNS协议、IP协议、TCP协议、UDP协议、三次握手、四次挥手)
  18. Android显示MP3专辑封面
  19. 测试学习12(测试分类及测试方法)
  20. VC-VQA: Visual Calibration Mechanism for Visual Question Answering (VQA的视觉校准机制)

热门文章

  1. Ngnix中的fastcgi參数性能优化和解释
  2. C#使用并行任务库(TPL)
  3. Web—09-正则表达式
  4. 使用百度编辑器--ueditor,后台接收提交编辑的内容,HTML不见了, 赋值不了,赋值之后,html暴露出来了??...
  5. Python 第七篇:socket编程
  6. 使用C#控制远程计算机的服务[转]
  7. c++时间函数及转换
  8. caffe---之scale层
  9. html转pdf后修改,pdf转换器smallpdf转成HTML后怎么排版
  10. python怎么爬取知乎回答并制作词云_使用python爬取流浪地球影评并制作词云,看看别人都说了些说什么...