如何在Java中扩展最终类 [英] How to extend a final class in Java

查看:287
本文介绍了如何在Java中扩展最终类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我现在面临的问题。
我有一个类,假设 Foo ,这个类定义了一个名为 getBar 的方法,它返回一个 Bar 实例。类 Bar Foo 中定义,并且声明为 public static final 。我想要做的是定义一个扩展 Foo 的类 MyFoo ,但我还要扩展通过添加我自己的功能(方法,属性等),使用 MyBar
我还希望 getBar 返回 MyBar

Here is my problem actually I'm facing now. I have a class, let's say Foo and this class defines a method called getBar that returns a Bar instance. The class Bar is defined inside Foo and and is declared public static final. What I want to do is to define a class MyFoo that extends Foo but I also want to extend Bar with MyBar by adding my own functionalities (methods, properties,etc). I also want getBar to return MyBar.

问题是 Bar 是最终的。以下是我想要做的事情的说明:

The problem is Bar is final. Here is an illustration of what I want to do :

public class Foo {

  Bar bar = new Bar();

  public Bar getBar(){
      return bar;
  } 

   ....

   public static final class Bar {

    } 
}

我想做的是:

public class MyFoo extends Foo {


   public MyBar getBar(){ // here I want to return an instance of MyBar

   }

  public static final class MyBar extends Bar { // That's impossible since Bar is final

  } 
}

我如何实现这一目标?

我在我的问题中添加了更多细节。
我实际上正在为Jenkins开发一个插件,在搜索之后,我发现有一个插件可以提供我想要的基本功能, Foo 酒吧。我想自定义 Foo Bar ,因此它可以满足我的需求。
Foo是插件的主要类,并扩展了一个名为Builder的类。 Foo定义了一个名为 perform 的方法,其中包含执行构建的所有操作。
Bar 包含用于配置目的的操作,Foo需要Bar来获取这些信息。

I am adding more details to my question. I am actually developing a plugin for Jenkins and after searching, I realize there is a plugin that offers the base functionalities that I want to do, Foo and Bar. I want to custom Foo and Bar, so it can fits my needs. Foo is the main class of the plugin and extends a class called Builder. Foo defines a method called perform which contains all the operations for performing a build. Bar contains operations for configurations purposes and Foo needs Bar to get those informations.

所以我想做的是用 MyFoo 扩展Foo并使用 MyBar 。如您所见, MyFoo 需要通过调用 getBar();

So what I want to do is to extend Foo with MyFoo and add some configurations with MyBar. And as you see, MyFoo will need to get those configuations by calling getBar();

第二件事是我无法控制 MyFoo MyBar 的实例化

The second thing is I have no control of the instanciation of MyFoo and MyBar

阻止我的是,我无法扩展Bar。我怎样才能做到这一点?

What blocks me, is that I cannot extend Bar. How can I achieve that?

推荐答案

基本上你不能。看起来设计 Bar 的开发人员不打算扩展它 - 所以你不能扩展它。你需要寻找一个不同的设计 - 可能是一个不使用继承的设计......当然,假设你不能改变现有的代码。

You can't, basically. It looks like the developer who designed Bar intended for it not to be extended - so you can't extend it. You'll need to look for a different design - possibly one which doesn't use inheritance so much... assuming you can't change the existing code, of course.

(如果不了解更多关于这里的真正目标 - 整个系统的设计,很难给出更具体的建议。)

(It's hard to give more concrete advice without knowing more about the real goal here - the design of the overall system.)

这篇关于如何在Java中扩展最终类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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