Java 扩展到另一个包中的类的最快方法,与当前包中的类同名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21403891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:30:52  来源:igfitidea点击:

Fastest way to extend to a class in another package, with the same name as one in the current package

javapackageextends

提问by George

I have in my package two classes: Animal and AnimalTest. I have in a different package another class Animal.

我的包中有两个类:Animal 和 AnimalTest。我在一个不同的包中有另一个类 Animal。

I want to have

我希望有

public class AnimalTest extends Animal

where Animal is the class from the other package. But when I write that, Eclipse automatically selects the class Animal from my current package.

其中 Animal 是来自另一个包的类。但是当我编写它时,Eclipse 会自动从我当前的包中选择类 Animal。

I found to ways to get what I want:

我找到了获得我想要的东西的方法:

  • by deleting the file, creating a class with the wizard and indicating the path of the Superclass
  • writing:

    public class AnimalTest extends com.the.other.package.Animal

  • then writing:

    import com.the.other.package.Animal

  • then changing the header back to:

    public class AnimalTest extends Animal

  • 通过删除文件,使用向导创建一个类并指示超类的路径
  • 写作:

    public class AnimalTest extends com.the.other.package.Animal

  • 然后写:

    import com.the.other.package.Animal

  • 然后将标题改回:

    public class AnimalTest extends Animal

and this time it is linked to the Animal class I want.

这一次它与我想要的 Animal 类相关联。

This seems very cumbersome to me and I am sure there is a better and simpler way to do it?

这对我来说似乎很麻烦,我确定有更好更简单的方法来做到这一点?

采纳答案by Aaron Digulla

Press Ctrl+Spaceafter extends Animal.

Ctrl+Space之后按extends Animal

Eclipse will display both classes in a dialog. Select the one you want and Eclipse will change the sources accordingly to make it happen.

Eclipse 将在对话框中显示这两个类。选择您想要的一个,Eclipse 将相应地更改源以使其发生。

If you want to insert the full name, double click on the Java type (i.e. Animal), open the context menu, press Y(Copy Qualified Name) and Ctrl+V.

如果要插入全名,请双击 Java 类型(即Animal),打开上下文菜单,按Y(Copy Qualified Name) 和Ctrl+V

回答by Ankit Lamba

I suggest you should use public class AnimalTest extends com.the.other.package.Animalbecause if you have two classes with same name in two different packages, and trying to use

我建议你应该使用,public class AnimalTest extends com.the.other.package.Animal因为如果你在两个不同的包中有两个同名的类,并且试图使用

import com.the.other.package.Animal
public class AnimalTest extends Animal

then your JVM should consider this Animalclass as your current package class, not from the package which you import.

那么您的 JVM 应该将此类Animal视为您当前的包类,而不是来自您导入的包。

回答by Suneet Bansal

@George : here is the answer:

@乔治:这是答案:

Suppose you have two classes: Animal & AnimalTest in com.onepackage. And another Animal class is in com.twopackage. Now if you want to extend Animal class of com.twopackage then simply you need to code like this:

假设您有两个类:Animal & AnimalTest in com.onepackage.json 。和另一个Animal class is in com.two包。现在,如果你想扩展com.two包的Animal 类,那么你只需要像这样编码:

class AnimalTest extends com.two.Animal{
}