在Java中覆盖用户库类 [英] Overriding User Library Classes in Java

查看:173
本文介绍了在Java中覆盖用户库类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用BlueJ for Mac编码java。我添加了stdlib.jar库(来自princeton http://introcs.cs.princeton.edu / java / stdlib / )。在添加这个库之前,我有我自己的类名为StdDraw.java(我在项目中使用的特定类)并复制/粘贴代码。我还调整了一些代码并添加了一些新行。

I code java with BlueJ for Mac. I have added the stdlib.jar library (From princeton http://introcs.cs.princeton.edu/java/stdlib/). Before added this library I had my own class named StdDraw.java (The specific class I was using on the project) and copy/pasted the code. I also adjusted some of the code and added some new lines. Since I cannot edit the libraries code, how may I override or extend library classes to add additional functionality?

推荐答案

因为库类不能编译库代码,是 final ,您不能扩展它们。另一个选择是将它们包装在您自己的类中。例如,您可以创建自己的图形类,它具有委派给它的 StdDraw 实例。对于许多方法,您可以简单地调用 StdDraw 实例的相应方法,或者您可以通过自己实现方法来模拟覆盖。

Since the library classes are final, you can't extend them. Another option is to wrap them in your own classes. For example, you can create your own drawing class that has an instance of StdDraw that it delegates to. For many of your methods you can simply call the corresponding method of the StdDraw instance, or you can simulate overriding by implementing methods yourself.

这篇关于在Java中覆盖用户库类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆