在Java中更改Import的名称,或导入两个同名的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2447880/
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 07:48:46  来源:igfitidea点击:

Change Name of Import in Java, or import two classes with the same name

javaimport

提问by Federer

In Python you can do a:

在 Python 中,您可以执行以下操作:

from a import b as c

How would you do this in Java, as I have two imports that are clashing.

您将如何在 Java 中执行此操作,因为我有两个发生冲突的导入。

采纳答案by Bozho

There is no import aliasing mechanism in Java. You cannot import two classes with the same name and use both of them unqualified.

Java 中没有导入别名机制。您不能导入具有相同名称的两个类,并且不限定地使用它们。

Import one class and use the fully qualified name for the other one, i.e.

导入一个类并为另一个类使用完全限定的名称,即

import com.text.Formatter;

private Formatter textFormatter;
private com.json.Formatter jsonFormatter;

回答by sepp2k

Java doesn't allow you to do that. You'll need to refer to one of the classes by its fully qualified name and only import the other one.

Java 不允许你这样做。您需要通过其完全限定名称来引用其中一个类,并且只导入另一个类。

回答by sfussenegger

It's probably worth noting that Groovy has this feature:

可能值得注意的是,Groovy 具有以下功能

import java.util.Calendar
import com.example.Calendar as MyCalendar

MyCalendar myCalendar = new MyCalendar()

回答by siegi

As the other answers already stated, Java does not provide this feature.

正如其他答案已经说明的那样,Java 不提供此功能。

Implementation of this feature has been requested multiple times, e.g. as JDK-4194542: class name aliasingor JDK-4214789: Extend import to allow renaming of imported type.

已多次请求实现此功能,例如JDK-4194542:类名别名JDK-4214789:扩展导入以允许重命名导入的类型

From the comments:

来自评论:

This is not an unreasonable request, though hardly essential. The occasional use of fully qualified names is not an undue burden (unless the library really reuses the same simple names right and left, which is bad style).

In any event, it doesn't pass the bar of price/performance for a language change.

这并不是一个不合理的要求,尽管几乎不是必需的。偶尔使用完全限定名称并不是一种过度的负担(除非库真的在左右重复使用相同的简单名称,这是不好的风格)。

无论如何,它不会超过语言更改的价格/性能标准。

So I guess we will not see this feature in Java anytime soon :-P

所以我想我们不会很快在 Java 中看到这个特性:-P

回答by 4thex

Actually it is possible to create a shortcut so you can use shorter names in your code by doing something like this:

实际上,可以通过执行以下操作来创建快捷方式,以便您可以在代码中使用较短的名称:

package com.mycompany.installer;
public abstract class ConfigurationReader {
    private static class Implementation extends com.mycompany.installer.implementation.ConfigurationReader {}
    public abstract String getLoaderVirtualClassPath();
    public static QueryServiceConfigurationReader getInstance() {
        return new Implementation();
    }
}

In that way you only need to specify the long name once, and you can have as many specially named classes you want.

这样,您只需指定一次长名称,就可以拥有任意数量的特殊命名类。

Another thing I like about this pattern is that you can name the implementing class the same as the abstract base class, and just place it in a different namespace. That is unrelated to the import/renaming pattern though.

我喜欢这种模式的另一件事是,您可以将实现类命名为与抽象基类相同的名称,并将其放在不同的命名空间中。不过,这与导入/重命名模式无关。

回答by Chris Suszyński

Today I filed a JEP draft to OpenJDK about this aliasing feature. I hope they will reconsider it.

今天我向 OpenJDK 提交了一份关于这个别名特性的 JEP 草案。我希望他们会重新考虑。

If you are interested, you can find a JEP draft here: https://gist.github.com/cardil/b29a81efd64a09585076fe00e3d34de7

如果您有兴趣,可以在这里找到 JEP 草案:https: //gist.github.com/cardil/b29a81efd64a09585076fe00e3d34de7