简体   繁体   English

在运行时扩展类

[英]Extending a class at runtime

I need to create a custom table UI. 我需要创建一个自定义表UI。 For future flexibility I would rather extend whatever object is returned by the call to UIManager.getUI() at the end of JTable.updateUI() . 为了将来的灵活性,我宁愿扩展JTable.updateUI()末尾对UIManager.getUI()的调用返回的任何对象。 So, I can't know what the class is called, or its path, until runtime. 因此,直到运行时,我才知道该类的名称或其路径。

Is this possible and how would I do it? 这可能吗,我会怎么做? I've never done anything to do with reflection, so if it involves that, please be gentle with me :-) I've had a look at other similar questions here and none of them seem to be what I need, or in an understandable way. 我从未做过任何与反思有关的事情,因此,如果涉及到这一点,请对我保持温柔:-)我在这里查看了其他类似的问题,但这些问题似乎都不是我所需要的,或者可以理解的方式。 Sorry if I've missed something :-) 抱歉,如果我错过了什么:-)

Edit: the structure I assume I'll need to use is something along these lines (this will go in my JTable descendant): 编辑:我认为我需要使用的结构是沿着这些方向的东西(这将出现在我的JTable后代中):

@Override
public void updateUI()
{
  super.updateUI();
  ComponentUI theUI = UIManager.getUI(this)[extended in some way with an overriding paint(){....}]
  setUI((TableUI)theUI);
}

It's the syntax for the part in square brackets that I'm missing. 这是我遗漏在方括号中的部分的语法。 What's in my mind is something kind of like the anonymous inner class declaration, perhaps: 我的想法有点像匿名内部类声明,也许是:

...
ComponentUI theUI = UIManager.getUI(this)
{
  public void paint(Graphics g, JComponent c)
  {
    ...
  }
}

I'm not saying the above exists, but is there some way to achieve it? 我并不是说上述内容存在,但是有什么方法可以实现?

if this is what you meant: 如果这是您的意思:

you can load a class at runtime using Class.forName. 您可以在运行时使用Class.forName加载类。 example: say we have an interface A which defines functiong foo() with many classes implementing it. 例如:说我们有一个接口A,它定义了functiong foo()以及许多实现它的类。 It's important to have same constructor for all of the classes (can do without it but it will be much complicated logic) 对所有类都使用相同的构造函数很重要(没有它可以做,但是逻辑会很复杂)

at runtime we get the name and possibly path (or we already know where our class should be by some other mean) of the class we want to use. 在运行时,我们获得了要使用的类的名称和可能的路径(或者我们已经知道该类的路径)。 so we can go like this: 这样我们就可以这样:

String class = getPath();
Class<?> toUse = Class.forName(class);
A instance = (A) toUse.getConstructors()[0].newInstance(args);
instance.foo(); //TADA

after op edit: ok your question is more clear now and I doubt my solution is what you're looking for. 经过op edit:好吧,您的问题现在更加清楚了,我怀疑我的解决方案是您要寻找的。 You can maybe create a class that will compose ComponentUI and use it to extend whatever you need. 您也许可以创建一个将组成ComponentUI的类,并使用它来扩展所需的内容。

something like this (really not sure if it helps since I have no knowledge about this whole JPanel etc thought this question was a mroe general one at first): 像这样的东西(真的不知道是否有帮助,因为我对整个JPanel不了解,等等,以为这个问题起初只是一个普遍的问题):

class B { //maybe class should extend or implement something, depends on what exactly is required
       private ComponentUI c;
       //other fields

       public B(ComponentUI c) {
           this.c = c;
           //other stuff you need done
       }

       //other methods you might need
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM