简体   繁体   English

如何在Eclipse中附加类的实际实现Java代码?

[英]How do I attach the actual implementation Java code of a class in Eclipse?

I have inherited a project that uses calls to an existing jar. 我继承了一个使用对现有jar的调用的项目。 The former programmer have had the fortune of obtaining a/the source code of the jar. 前程序员有幸获得了jar的源代码。 However the construction is rather complex. 但是,结构相当复杂。 I use an abstract class, say Size. 我使用一个抽象类,例如Size。 In the source code I find an implementation of that class, say SizeProxy. 在源代码中,我找到了该类的一个实现,例如SizeProxy。 It has an object of class SizeImplementation and any calls on Size.getInfo() will be implemented as a call to this.theSize.getInfo(). 它具有一个SizeImplementation类的对象,对Size.getInfo()的任何调用都将作为对此this.theSize.getInfo()的调用来实现。

In short I have managed to find the actual implementation of getInfo that actually does anything interesting. 简而言之,我设法找到了getInfo的实际实现,该实现实际上做了一些有趣的事情。 I would love to make it possible to go from "my" code o_size.getInfo() and go directly to the "implementation" in SizeImplementation.getInfo. 我希望可以从“我的”代码o_size.getInfo()中直接转到SizeImplementation.getInfo中的“实现”。 Right now I can't make Eclipse do that, possibly because the class names don't match. 现在,我不能让Eclipse这样做,可能是因为类名不匹配。 Please help! 请帮忙!

This is in general not possible because an interface can have several implementations, and an abstract class (or any class) can be extended by many classes. 通常这是不可能的,因为接口可以具有多个实现,并且抽象类(或任何类)可以由许多类扩展。 Take this example 举个例子

public void doSomething(boolean option){
    List<String> someList;
    if (option){
         list=new ArrayList<>();
    }else{
         list=new LinkedList<>();
    }

    list.add("Where do I go"); 

}

Were you to attempt to go to the implementing source directly from list.add("Where do I go"); 您是否试图直接从list.add("Where do I go");实施源list.add("Where do I go"); to the implementation of .add() where would you end up? .add()的实现,您将在哪里结束? ArrayList's or LinkedList's. ArrayList或LinkedList。

This is why you have to go via the interface or abstract class then choose your implemetation from there 这就是为什么您必须通过接口或抽象类然后从那里选择实现的原因

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

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