java,有没有办法可以导入另一个名称的类

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

java, is there a way we can import a class under another name

javasyntactic-sugarimport

提问by Pacerier

is there a way we can import a class under another name? Like if i have a class called javax.C and another class called java.C i can import javax.C under the name C1 and import java.C under the name C2.

有没有办法可以用另一个名字导入一个类?就像我有一个名为 javax.C 的类和另一个名为 java.C 的类,我可以在名称 C1 下导入 javax.C 并在名称 C2 下导入 java.C。

We can do something like this in C#:

我们可以在 C# 中做这样的事情:

using Sys=System;

or Vb:

或 VB:

Imports Sys=System

回答by Michael Borgwardt

No, there is nothing like that in Java. You can only import classes under their original name, and have to use the fully qualified name for all that you don't import (except those in java.langand the current class's package).

不,Java 中没有类似的东西。您只能以原始名称导入类,并且必须对所有未导入java.lang的类使用完全限定名称(除了在当前类的包中的那些)。

回答by Michiel Borkent

To be short, no, this isn't possible in Java.

简而言之,不,这在 Java 中是不可能的。

回答by irreputable

No. I think Java deliberatelyditched typedef. The good news is, if you see a type, you know what it is. It can't be an alias to something else; or an alias to an alias to ...

不,我认为 Java 是故意放弃的typedef。好消息是,如果你看到一个类型,你就会知道它是什么。它不能是别的东西的别名;或别名的别名...

If a new concept really deserves a new name, it most likely deserves a new type also.

如果一个新概念真的值得一个新名称,它很可能也值得一个新类型。

The usage example of Sys=Systemwill be frowned upon by most Java devs.

Sys=System大多数 Java 开发人员都不喜欢的使用示例。

回答by Rob Hoff

Java doesn't support static renaming. One idea is to subclass object in question with a new classname (but may not be a good idea because of certain side-effects / limitations, e.g. your target class may have the final modifier. Where permitted the code may behave differently if explicit type checking is used getClass()or instanceof ClassToRename, etc. (example below adapted from a different answer)

Java 不支持静态重命名。一个想法是使用新的类名对有问题的对象进行子类化(但由于某些副作用/限制,这可能不是一个好主意,例如您的目标类可能具有 final 修饰符。如果显式类型检查,代码可能会在允许的情况下表现不同使用getClass()orinstanceof ClassToRename等(下面的示例改编自不同的答案)

class MyApp {

  public static void main(String[] args) {
    ClassToRename parent_obj = new ClassToRename("Original class");
    MyRenamedClass extended_obj_class_renamed = new MyRenamedClass("lol, the class was renamed");
    // these two calls may be treated as the same
    // * under certain conditions only *
    parent_obj.originalFoo();
    extended_obj_class_renamed.originalFoo();
  }  

  private static class ClassToRename {
    public ClassToRename(String strvar) {/*...*/}
    public void originalFoo() {/*...*/}
  }

  private static class MyRenamedClass extends ClassToRename {
    public MyRenamedClass(String strvar) {super(strvar);}
  }

}