Java 导入两个同名的类。如何处理?

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

Importing two classes with same name. How to handle?

java

提问by gameover

Say I've a code like:

假设我有一个类似的代码:

import java.util.Date;
import my.own.Date;

class Test{

  public static void main(String [] args){

    // I want to choose my.own.Date here. How?
    ..
    // I want to choose util.Date here. How ?

  }
}

Should I be full qualified class names? Can I get rid of the import statements? Is such a scenario common in real world programming?

我应该是完全限定的类名吗?我可以摆脱导入语句吗?这样的场景在现实世界的编程中常见吗?

采纳答案by Ellie P.

You can omit the import statements and refer to them using the entire path. Eg:

您可以省略导入语句并使用整个路径引用它们。例如:

java.util.Date javaDate = new java.util.Date()
my.own.Date myDate = new my.own.Date();

But I would say that using two classes with the same name and a similiar function is usually not the best idea unless you can make it really clear which is which.

但我会说,使用两个具有相同名称和相似函数的类通常不是最好的主意,除非您能明确说明哪个是哪个。

回答by Chii

use the fully qualified name instead of importing the class.

使用完全限定名称而不是导入类。

e.g.

例如

//import java.util.Date; //delete this
//import my.own.Date;

class Test{

   public static void main(String [] args){

      // I want to choose my.own.Date here. How?
      my.own.Date myDate = new my.own.Date();

      // I want to choose util.Date here. How ?
      java.util.Date javaDate = new java.util.Date();
   }
}

回答by D.C.

Yes, when you import classes with the same simple names, you must refer to them by their fully qualified class names. I would leave the import statements in, as it gives other developers a sense of what is in the file when they are working with it.

是的,当您导入具有相同简单名称的类时,您必须通过它们的完全限定类名称来引用它们。我会保留导入语句,因为它让其他开发人员在使用文件时了解文件中的内容。

java.util.Data date1 = new java.util.Date();
my.own.Date date2 = new my.own.Date();

回答by Yatendra Goel

This scenario is not so common in real-world programming, but not so strange too. It happens sometimes that two classes in different packages have same name and we need both of them.

这种情况在现实世界的编程中并不常见,但也不那么奇怪。有时会发生不同包中的两个类具有相同的名称,而我们同时需要它们。

It is not mandatory that if two classes have same name, then both will contain same functionalities and we should pick only one of them.

如果两个类具有相同的名称,那么它们将包含相同的功能,我们应该只选择其中一个。

If we need both, then there is no harm in using that. And it's not a bad programming idea too.

如果我们两者都需要,那么使用它没有什么坏处。这也不是一个糟糕的编程想法。

But we should use fully qualified names of the classes (that have same name) in order to make it clear which class we are referring too.

但是我们应该使用类的完全限定名称(具有相同的名称),以便明确我们所指的是哪个类。

:)

:)

回答by Peter Lawrey

If you have your own date class you should distinguish it form the built in Date class. i.e. why did you create your own. Something like ImmutableDate or BetterDate or NanoDate, even MyDate would indicate why you have your own date class. In this case, they will have a unique name.

如果您有自己的日期类,则应将其与内置的 Date 类区分开来。即你为什么要创建自己的。像 ImmutableDate 或 BetterDate 或 NanoDate 之类的东西,甚至 MyDate 都会表明你为什么有自己的日期类。在这种情况下,它们将具有唯一的名称。

回答by Cheruvim

Another way to do it is subclass it:

另一种方法是将其子类化:

package my.own;

public class FQNDate extends Date {

}

And then import my.own.FQNDate in packages that have java.util.Date.

然后在包含 java.util.Date 的包中导入 my.own.FQNDate。

回答by Randy Wilson

I hit this issue when, for example, mapping one class to another (such as when switching to a new set of classes to represent person data). At that point, you need both classes because that is the whole point of the code--to map one to the other. And you can't rename the classes in either place (again, the job is to map, not to go change what someone else did).

例如,当将一个类映射到另一个类时(例如切换到一组新类来表示个人数据时),我遇到了这个问题。在这一点上,您需要两个类,因为这是代码的全部重点——将一个映射到另一个。而且你不能在任何地方重命名类(同样,工作是映射,而不是改变其他人所做的)。

Fully qualified is one way. It appears you can't actually include both import statements, because Java gets worried about which "Person" is meant, for example.

完全合格是一种方式。看起来您实际上不能同时包含两个 import 语句,因为例如,Java 会担心“Person”是指哪个。

回答by Anuraj

You can import one of them using import. For all other similar class , you need to specify Fully qualified class names. Otherwise you will get compilation error.

您可以使用 import 导入其中之一。对于所有其他类似的类,您需要指定完全限定的类名。否则你会得到编译错误。

Eg:

例如:

import java.util.Date;

class Test{

  public static void main(String [] args){

    // your own date
    my.own.Date myOwndate ;

    // util.Date
    Date utilDate;
  }
}

回答by Bondhan Novandy

I just had the same problem, what I did, I arranged the library order in sequence, for example there were java.lang.NullPointerException and javacard.lang.NullPointerException. I made the first one as default library and if you needed to use the other you can explicitly specify the full qualified class name.

我刚遇到同样的问题,我是怎么做的,我是按顺序排列库的顺序,比如有java.lang.NullPointerException和javacard.lang.NullPointerException。我将第一个作为默认库,如果您需要使用另一个,您可以明确指定完全限定的类名。

回答by manfall19

If you really want or need to use the same class name from two different packages, you have two options:

1-pick one to use in the import and use the other's fully qualified class name:

如果您真的想要或需要使用来自两个不同包的相同类名,您有两种选择:

1-选择一个在导入中使用,并使用另一个的完全限定类名:

import my.own.Date;

class Test{

     public static void main(String[] args){

        // I want to choose my.own.Date here. How?
        //Answer:
        Date ownDate = new Date();

        // I want to choose util.Date here. How ?
        //Answer:
        java.util.Date utilDate = new java.util.Date();

     }
}



2-always use the fully qualified class name:

2-始终使用完全限定的类名:

//no Date import
class Test{

  public static void main(String[] args){

    // I want to choose my.own.Date here. How?
    //Answer:
     my.own.Date ownDate = new my.own.Date();
    // I want to choose util.Date here. How ?
    //Answer:
     java.util.Date utilDate = new java.util.Date();

  }
}