简体   繁体   English

如果 2 个类有相同的 package 语句怎么办

[英]What to do if 2 classes have the same package statement

I'm running into a problem where 2 classes from 2 different packages (owned by the same group) have the same name and package statement我遇到了一个问题,其中来自 2 个不同包(属于同一组)的 2 个类具有相同的名称和 package 语句

Package 1 Package 1

package com.placeholder.constants.Constants

public class Constants{}

Package 2 Package 2

package com.placeholder.constants.Constants

public class Constants{}

If I'm working on Package 1 but want to reference this Constants class in Package 2, is this even possible?如果我正在研究 Package 1 但想在 Package 2 中引用此常量 class ,这是否可能?

Cannot duplicate class name within same package不能在同一 package 中复制 class 名称

where 2 classes from 2 different package其中来自 2 个不同的 package 的 2 个类

But they are not two different packages.但它们不是两个不同的包。 You've used the same package name for both.您为两者使用了相同的 package 名称。

want to reference this Constants class in Package 2想在 Package 2 中引用这个常量 class

Use either:使用任一:

  • A different package name不同的 package 名称
  • A different name for your new class您的新 class 的不同名称

You cannot define two classes with the same name in the same package, for obvious reasons.由于显而易见的原因,您不能在同一个 package 中定义两个具有相同名称的类。

The purpose of packages in Java is to create a namespace . Java 中的包的目的是创建一个命名空间 Your duplicate class name violates that namespace.您重复的 class 名称违反了该命名空间。

Design problems设计问题

package com.placeholder.constants.Constants

That is not a proper package name.这不是正确的 package 名称。 You've mixed the class name into the package name.您已将 class 名称混合到 package 名称中。 The package should be package com.placeholder.constants . package 应该是package com.placeholder.constants

This cross-naming problem of yours raises the possibility that you have a less than optimal class design.您的这个交叉命名问题增加了您的 class 设计不是最优的可能性。 You might want to pause a moment to look at the bigger picture.您可能想暂停片刻,看看更大的图景。

And if you have that many constants to name, I wonder if some of those should really be enum types.如果你有那么多常量要命名,我想知道其中一些是否真的应该是enum类型。 See tutorial by Oracle .请参阅Oracle 的教程 Tip: In Java 16 and later, enums can be defined locally, within an a method — a new alternative to being defined as a nested class or defined as a separate class.提示:在 Java 16 及更高版本中,枚举可以在本地定义,在一个方法内 - 定义为嵌套 class 或定义为单独的 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6DZ 的新替代方法。

You are facing a name conflict.您正面临名称冲突。 If possible, try to enter the full name of the package.class_location如果可能,尝试输入package.class_location的全名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我是否必须在 AOSP java 中包含来自同一 package 的类? - Do I have to include classes from the same package in AOSP java? 是否可以在同一包中有两个具有相同名称的类? - Is it possible to have 2 classes with the same name and in the same package? Java - 我是否必须在包中包含的所有类中注释包? - Java - Do I have to annotate a package in all classes that are contained by the package? 同一源代码包中是否可以包含.class和.java类? - is it possible to have .class and .java classes inside the same source code package? 同一个类的两个类是否具有相同的哈希码,它们被认为是相同的吗? - Do two classes of the same class have the same hashcode and are they considered equal? 同一个包中的类 - Classes in same package 如果所有类不在同一个包中,则Spring @autowired不起作用 - Spring @autowired do not work if all classes are not in the same package 当我们在Java中有两个具有相同名称的类时会发生什么? - what happens when we have two classes with same names in java? 两个类具有相同的API但实现不同意味着什么? - What does it mean for two classes have the same API but different implementations? 如果两个库的类具有相同的类和包名称供其内部使用 - if two libraries have classes that have the same class& package name for their internal use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM