java 包与类型发生冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12236909/
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
The package collides with a type
提问by Solo Omsarashvili
Possible Duplicate:
eclipse 3.4 (ganymede) package collision with type
I'm new in java, but i tried to write a script for a game Lineage2.
我是 Java 新手,但我尝试为游戏 Lineage2 编写脚本。
heres a code:
这是一个代码:
package ZergZ.ZTeleport;
import javolution.util.FastMap;
import com.l2jserver.Config;
import com.l2jserver.gameserver.handler.IVoicedCommandHandler;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jserver.gameserver.model.Location;
import com.l2jserver.gameserver.handler.VoicedCommandHandler;
public class ZTeleport implements IVoicedCommandHandler
{
private static final String[] VOICED_COMMANDS =
{
"teleport"
};
@Override
public boolean useVoicedCommand(String command, L2PcInstance activeChar, String params)
{
if (activeChar == null)
return false;
if (params.equalsIgnoreCase("aden"))
{
activeChar.teleToLocation(147736,-56243,-2781);
}
if (params.equalsIgnoreCase("gracia"))
{
activeChar.teleToLocation(-186742,244167,2675);
}
if (params.equalsIgnoreCase("pvp1"))
{
activeChar.teleToLocation(147736,-56243,-2781);
}
if (params.equalsIgnoreCase("pvp2"))
{
activeChar.teleToLocation(179337,221937,4475);
}
}
@Override
public String[] getVoicedCommandList()
{
return VOICED_COMMANDS;
}
}
when server starts java says:
1. ERROR in \ZTeleport.java (at line 17)
package ZergZ.ZTeleport;
^^^^^^^^^^^^^^^^^^^^
The package ZergZ.ZTeleport collides with a type
当服务器启动 java 时说: 1. ERROR in \ZTeleport.java (at line 17)
package ZergZ.ZTeleport;
^^^^^^^^^^^^^^^^^^^^
封装ZergZ.ZTeleport碰撞类型
the script is situated in ZergZ/ZTeleport.java
该脚本位于 ZergZ/ZTeleport.java
I'll give you another script which works fine:
我会给你另一个运行良好的脚本:
package custom.HeroCirclet;
import com.l2jserver.gameserver.model.actor.L2Npc;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.quest.Quest;
import com.l2jserver.gameserver.model.quest.QuestState;
public class HeroCirclet extends Quest
{
______
}
Thanks.
谢谢。
回答by Buhake Sindi
It's because you have a class named ZTeleport
in the ZergZ package and a package named ZergZ.ZTeleport
.
这是因为你有一个名为ZTeleport
ZergZ 包的类和一个名为ZergZ.ZTeleport
.
The package
name is basically the project directory where the Java file is situated.
该package
名称基本上是 Java 文件所在的项目目录。
That means if ZTeleport.java
is in ZergZ
directory, then the package name is
这意味着如果ZTeleport.java
在ZergZ
目录中,那么包名是
package ZergZ;
You don't specify the class name on package declaration and directory are separated with a .
and not directory folder token.
您没有在包声明中指定类名,并且目录用.
和 not 目录文件夹标记分隔。
回答by Dave Costa
You say "the script is situated in ZergZ/ZTeleport.java". This implies that the class ZTeleport
belongs to the package ZergZ
. But you have declared it as belonging to a different package, ZergZ.ZTeleport
.
您说“脚本位于 ZergZ/ZTeleport.java 中”。这意味着该类ZTeleport
属于包ZergZ
。但是您已将其声明为属于不同的包ZergZ.ZTeleport
.
In your second example, I would bet that the source file is located in custom/HeroCirclet/HeroCirclet.java, which matches its package declaration, and does not create a naming conflict.
在您的第二个示例中,我敢打赌源文件位于 custom/HeroCirclet/HeroCirclet.java 中,它与其包声明相匹配,并且不会产生命名冲突。
You either need to move the source file (people normally don't call java source files "scripts", btw) into a directory that matches its declared package, or change the package declaration to match its location.
您需要将源文件(人们通常不会将 java 源文件称为“脚本”,顺便说一句)移动到与其声明的包匹配的目录中,或者更改包声明以匹配其位置。
回答by Eusebius
The collision here is between your package name and your class name, which are the same. If you stick to the usual naming conventions (naming your packages with a starting lower case and your classes with a starting upper case), you should avoid such situations.
这里的冲突是你的包名和你的类名之间的冲突,它们是相同的。如果您坚持通常的命名约定(用小写开头的包命名,用大写开头的类命名),你应该避免这种情况。
回答by hqt
You should follow Java naming conventions. Change your package into:
您应该遵循 Java 命名约定。将您的包更改为:
package zergZ.zTeleport; // all name is begin with lower
// no change, but for clearer : all class name should begin with higher character
public class ZTeleport implements IVoicedCommandHandler
{
}
After that, for sure, you should refresh or/and rebuild your project to see it works.
在那之后,当然,您应该刷新或/和重建您的项目以查看它是否有效。
Hope this help :)
希望这有帮助:)
回答by Bharat Sinha
If you see the error it says...
如果您看到错误,它会说...
package ZergZ.ZTeleport;
^^^^^^^^^^^^^^^^^^^^
The package ZergZ.ZTeleport collides with a type
So basically you have created a conflict between the way you have named your class and the package. If you stick to java conventions you could name them as:
所以基本上你已经在你命名你的类和包的方式之间产生了冲突。如果您坚持 Java 约定,您可以将它们命名为:
// Notice the first character of package name is small character
package zergZ.zTeleport;
// Class Name's first character is capital.
public class ZTeleport implements IVoicedCommandHandler{
..
}