java中的类或方法别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2204157/
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
class or method alias in java
提问by user121196
I have long java class and method names
我有很长的java类和方法名
LONGGGGGGGGGGGGGGGClass.longggggggggggggggggggggggggMethod();
I want to alias it to
g.m();
in another class
can this be done?
我想将其别名为
g.m();
另一个类可以这样做吗?
回答by Eric J.
You can use inheritance or encapsulation to wrap the original class.
您可以使用继承或封装来包装原始类。
class g extends LONGCLASS
{
void a() { super.loooonnng(); }
}
or
或者
class g
{
private LONGCLASS lc;
void a() { lc.loooonnng(); }
}
回答by Thorbj?rn Ravn Andersen
No.
不。
Wrap it in a method with a name you like better.
用你更喜欢的名字把它包装在一个方法中。
回答by Drew Wills
Actually there isa way to get 1/2 of what you're after.
其实,有是一个办法让你以后1/2。
Looking at your example:
看看你的例子:
LONGGGGGGGGGGGGGGGClass.longggggggggggggggggggggggggMethod();
It appears that longggggggggggggggggggggggggMethod
is static. (If it weren't, you'd be prefixing it with a variable name, which you control the size of.)
看来longggggggggggggggggggggggggMethod
是静态的。(如果不是,你会在它前面加上一个变量名,你可以控制它的大小。)
You can use Java's static import featureto 'alias' or importthe static methods of the LONGGGGGGGGGGGGGGGClass
into your own class' namespace. Instead of the above code, you would only have to write this:
您可以使用Java 的静态导入功能“别名”或将的静态方法LONGGGGGGGGGGGGGGGClass
导入您自己的类的命名空间。而不是上面的代码,你只需要写这个:
longggggggggggggggggggggggggMethod();
回答by Bill K
For one thing, you should rarely be typing the class name. You might have something like this:
一方面,您应该很少键入类名。你可能有这样的事情:
import DamnLongPackageNameThatIDontLikeTyping;
class MyCoolClass()
{
DamnLongClassNameThatIDontLikeTyping dlc=new DamnLongClassNameThatIDontLikeTyping();
dlc.this();
dlc.that();
dlc.tOther();
dlc.damnLongAnnoyingMethodNameStillHasToBeTypedEveryTime();
}
Okay, so that's not great, but you shouldn't be typing the entire class name very often, just when you first declare it; and the package import makes it so you don't have to type: DamnLongPackageNameThatIDontLikeTyping.DamnLongClassNameThatIDontLikeTyping
every time.
好的,这不是很好,但是您不应该经常键入整个类名,就在您第一次声明它时;并且包导入使您不必DamnLongPackageNameThatIDontLikeTyping.DamnLongClassNameThatIDontLikeTyping
每次都键入:。
Still, that can be annoying to type. Enter the editor. If you aren't using Eclipse, Netbeans or IntelliJ then you really need to stop reading right now and go install it--load up your project. I'll wait....
尽管如此,打字还是很烦人的。进入编辑器。如果您没有使用 Eclipse、Netbeans 或 IntelliJ,那么您现在真的需要停止阅读并安装它——加载您的项目。我会等待....
Seriously. Go get it. The rest of this won't be any fun without it.
严重地。去实现它(梦想);去得到它(东西。没有它,剩下的事情就不会有乐趣了。
Now, the really neat thing is that to get what I typed above, you just do this:
现在,真正巧妙的是要获得我上面输入的内容,您只需执行以下操作:
class MyCoolClass()
{
DLC<ctrl-space>
After typing that, your file will look like this:
输入后,您的文件将如下所示:
import DamnLongPackageNameThatIDontLikeTyping;
class MyCoolClass()
{
DamnLongClassNameThatIDontLikeTyping<your cursor here>
Note that you didn't type damn long ANYTHING, just DLC It figured out what class you wanted to import, added an import for it and stuck the class in there. (You may have to choose from a list of classes if there is more than one match).
请注意,您并没有输入该死的任何东西,只是 DLC 它确定了您想要导入的类,为其添加导入并将类卡在那里。(如果有多个匹配项,您可能必须从类列表中进行选择)。
On top of that, once you have an object named dlc instantiated you can type:
最重要的是,一旦您实例化了一个名为 dlc 的对象,您就可以输入:
dlc.<ctrl-space>
and get a list of methods in that class. NEVER AGAIN TYPE A METHOD NAME. If there are a kagillion methods in your class, don't scroll over them, type: dlc.dLAM<ctrl-space>
to get dlc.damnLongAnnoyingMethodNameStillHasToBeTypedEveryTime();
dlc.<ctrl-space>
并获取该类中的方法列表。永远不要再输入方法名称。如果您的类中有 kagillion 方法,请不要滚动它们,键入:dlc.dLAM<ctrl-space>
以获取dlc.damnLongAnnoyingMethodNameStillHasToBeTypedEveryTime();
Never type a long method name again.
永远不要再输入长的方法名称。
No long method names, no long class names, no long package names. Yet you get extremely readable methods, packages and classes. This is why java programmers tend to use these long names, we also try to remember that we are coding for the next guy and don't want him to have to run all over our code trying to figure out what:
没有长方法名,没有长类名,没有长包名。但是你会得到非常可读的方法、包和类。这就是 Java 程序员倾向于使用这些长名称的原因,我们还尝试记住我们正在为下一个人编码,并且不希望他必须运行我们的所有代码试图找出什么:
g.m();
refers to -- forcing them to remember that in this class it means GreatClass.motion
, but in the next class it means Grey.modifyColor
-- that would be really cruel.
g.m();
指的是——强迫他们记住,在这堂课中它意味着GreatClass.motion
,但在下一堂课中它意味着Grey.modifyColor
——那将是非常残酷的。
Java being statically typed places a LOT of power into the editor. It can do things that you can't even dream of doing with dynamically typed languages, and you should play to the strength of your language to be an effective programmer -- not try to fit each language into some style you learned from using another language.
静态类型的 Java 为编辑器提供了很多功能。它可以做你用动态类型语言做梦都做不到的事情,你应该发挥你的语言的力量成为一名有效的程序员——不要试图将每种语言融入你从使用另一种语言中学到的某种风格.
Note that this works for static methods as well...
请注意,这也适用于静态方法......
DLC<ctrl-space>.dLM<ctrl-space>
would be replaced by a call to DamnLongClass.damnLongMethod()
, and it would even include the parens for you in 9 keystrokes.
DLC<ctrl-space>.dLM<ctrl-space>
将被调用替换为DamnLongClass.damnLongMethod()
,它甚至会在 9 次击键中为您包含括号。
回答by Stephen C
The Java language provides no aliasing mechanism.
Java 语言不提供别名机制。
However, you could ease your "pain" somewhat by some combination of the following:
但是,您可以通过以下某种组合来缓解您的“痛苦”:
For static methods, you can use static imports to avoid having the long class name.
You could declare your own convenience class with a short name and short method names, and implement the static methods to delegate to the real methods like:
public static void shortName(...) { VeryLongClassName.veryLongMethodName(...); }
For regular methods, you could implement a Wrapper class, or a subclass with more convenient method names. However, both have downsides from the maintenance and (depending on your JVM) performance perspectives.
对于静态方法,您可以使用静态导入来避免类名过长。
您可以使用短名称和短方法名称声明自己的便利类,并实现静态方法以委托给真正的方法,例如:
public static void shortName(...) { VeryLongClassName.veryLongMethodName(...); }
对于常规方法,您可以实现 Wrapper 类,或具有更方便的方法名称的子类。但是,两者在维护和(取决于您的 JVM)性能方面都有缺点。
But lets step back:
但让我们退后一步:
If the real problem is that you are just fed up with typing long names, a solution is to use a modern IDE that supports completion of names as you type them. See @BillK's answer for example.
If the real problem is that you are fed up with the long names taking to much space, a solution is to use a wider screen / longer lines. Most monitors are big enough to display 120 character (or more) wide source code with no eye strain.
If neither of the above is the answer, consider just refactoring the offending code to use sensible (i.e. shorter) class and method names. Once again, a modern IDE can handle this kind of refactoring quickly and safely.
如果真正的问题是您只是厌倦了键入长名称,则解决方案是使用支持在键入名称时完成名称的现代 IDE。例如,请参阅@BillK 的回答。
如果真正的问题是您厌倦了占用大量空间的长名称,则解决方案是使用更宽的屏幕/更长的行。大多数显示器都足够大,可以显示 120 个字符(或更多)宽的源代码而不会造成眼睛疲劳。
如果以上都不是答案,请考虑重构违规代码以使用合理(即更短)的类和方法名称。再一次,现代 IDE 可以快速安全地处理这种重构。
On the last point, I would consider that the overly long class names and method names are bad style. IMO, you are justified in taking the time to fix them yourself, or suggesting that they be fixed, especially if they constitute a "public" API for some library or module.
最后一点,我会认为过长的类名和方法名是不好的风格。IMO,您有理由花时间自己修复它们,或者建议修复它们,尤其是当它们构成某些库或模块的“公共”API 时。
回答by Ivan Skalauh
public class Shorten
{
public static final Shorten m = new Shorten();
public int a(params)
{
return some_method_with_long_name(params);
}
public void b()
{
// whatever static code you want
}
}
In your main code then:
在您的主要代码中:
import static mypackage.Shorten.m;
...
int res = m.a(params);
m.b();
...
This way you effectively alias any static stuff you want, while avoiding warnings.
通过这种方式,您可以有效地为您想要的任何静态内容设置别名,同时避免警告。
回答by bvdb
Not supported in Java.
Java 不支持。
There is an enhancement ticket (7166917)for adding aliases for importswhich would be helpful. The idea is this :
有一个增强票 (7166917)用于为导入添加别名,这将很有帮助。这个想法是这样的:
import a.very.lng.pckage.* as shortpckg
import my.pckage.IsAVeryLongClassName as MyShort
回答by Sean Counterman
I only ran a simple test but I defined an inner class variable. I'm not an expert nor do I know the consequences of doing this but I obtained positive results.
我只运行了一个简单的测试,但我定义了一个内部类变量。我不是专家,也不知道这样做的后果,但我获得了积极的结果。
package a.b;
public class Library {
public static String str;
}
Now write a class to access the static variables from Library
现在编写一个类来访问库中的静态变量
package a.b;
public class Access {
public class Short extends Library {}
Short.str;
}