Handle Classes With the Same Name in Java – 在Java中处理同名的类

最后修改: 2022年 9月 5日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

Class naming in Java follows an international convention called Upper Camel Case syntax, like the major programming languages. However, when it comes to handling classes with the same name, there’s a challenge.

在Java中,类的命名遵循一种叫做Upper Camel Case语法的国际惯例,和主要的编程语言一样。然而,在处理同名的类时,有一个挑战。

Since the early release of JDK in 1998, there’s been a debate about how to solve this unusual situation. Here’s JDK-4194542, the first opened bug on this topic, and since then, the JDK development team’s recommendation has been to use the fully-qualified class names. Nevertheless, there are no plans for the JDK to have any shortly a feature that allows this kind of usage.

自 1998 年发布 JDK 的早期版本以来,人们一直在讨论如何解决这种不寻常的情况。这里有JDK-4194542,这是关于这个主题的第一个公开的错误,从那时起,JDK 开发团队的建议是使用完全限定的类名。然而,目前还没有计划让 JDK 在短期内拥有允许这种用法的功能。

Lately, in August 2019, the Java developers community raised a new proposal (JEP) on how to solve this situation, and it’s gaining more support from Java developers worldwide.

最近,在2019年8月,Java开发者社区就如何解决这种情况提出了一个新的建议(JEP),并得到了全球Java开发者的更多支持。

In this tutorial, we’ll discuss strategies and recommendations for dealing with classes with the same name.

在本教程中,我们将讨论处理同名类的策略和建议。

2. Defining the Class

2.定义班级

First, let’s create a class called Date inside a custom package com.baeldung.date.

首先,让我们在自定义包com.baeldung.date.中创建一个名为Date的类。

package com.baeldung.date;

public class Date {

    private long currentTimeMillis;

    public Date() {
        this(System.currentTimeMillis());
    }

    public Date(long currentTimeMillis) {
        this.currentTimeMillis = currentTimeMillis;
    }

    public long getTime() {
        return currentTimeMillis;
    }
}

3. Fully Qualified Class Names

3.完全合格的类名

We’ll use this approach to avoid collisions when this type of usage is isolated and not frequently repeated. Nevertheless, using fully qualified names is usually considered a poor style.

当这种类型的用法是孤立的且不经常重复时,我们将使用这种方法来避免碰撞。然而,使用完全限定的名称通常被认为是一种糟糕的风格。

Let’s look at how to use it, especially if the package name is short and descriptive could make the code more expressive, therefore reducing confusion and increasing readability.

让我们来看看如何使用它,尤其是如果包的名字很短,描述性强可以使代码更具表现力,从而减少混乱,增加可读性。

On the other hand, it helps to debug when the objects inside were used are too large classes or methods:

另一方面,当里面使用的对象是太大的类或方法时,它有助于调试。

public class DateUnitTest {

    @Test
    public void whenUsingFullyQualifiedClassNames() {

        java.util.Date javaDate = new java.util.Date();
        com.baeldung.date.Date baeldungDate = new com.baeldung.date.Date(javaDate.getTime());

        Assert.assertEquals(javaDate.getTime(), baeldungDate.getTime());
    }
}

4. Import the Most Used One

4.导入最常用的一个

We import the one we most use and utilize the least used one with a full classpath, as this is the common technique and a best practice among Java developers:

我们导入我们最常用的那个,并利用最不常用的那个,并提供完整的classpath,因为这是Java开发者的常用技术和最佳实践:

import java.util.Date;

public class DateUnitTest {

    @Test
    public void whenImportTheMostUsedOne() {

        Date javaDate = new Date();
        com.baeldung.date.Date baeldungDate = new com.baeldung.date.Date(javaDate.getTime());

        Assert.assertEquals(javaDate.getTime(), baeldungDate.getTime());
    }
}

5. Conclusion

5.总结

In this article, we illustrated the two possible approaches regarding the use of classes having the same name depending on particular situations and observed the main difference between them.

在这篇文章中,我们说明了两种可能的方法,即根据特定情况使用具有相同名称的类,并观察它们之间的主要区别。

As always, the complete code samples for this article can be found over on GitHub.

一如既往,本文的完整代码样本可以在GitHub上找到