导入的常量类字段的简称 [英] Short name for imported constant class fields

查看:46
本文介绍了导入的常量类字段的简称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有数百个配置常量的Java程序:

I have a java program with hundreds of configuration constants:

public static final String C1="C1";
public static final String C2="C2";

由于它们太多,因此我将它们放在一个单独的类中,即 MyClassConstants .现在,我需要在 MyClass 上使用它们:

Since there are so many of them, I've put them into a separate class, MyClassConstants. Now, I need to use them on MyClass:

import mynamespace.MyClassConstants;
myMethod( MyClassConstants.C1, MyClassConstants.C2 );

这变得非常冗长,所以我想知道是否可以通过某种方式直接导入字段:

This gets very verbose very fast, so I was wondering if it was possible to somehow import the fields directly:

import mynamespace.MyClassConstants.*; 
myMethod( C1, C2 ); //doesn't work

或者至少要重命名导入:

Or at the very least, rename the import:

import mynamespace.MyClassConstants as C; //javac hates me
myMethod( C.C1, C.C2 );

但是似乎后来的方法是不可能的

有没有办法做到这一点,并且对于常量仍然具有有意义的类名?还是应该使用其他方法?

Is there a way to do this and still have a meaningful class name for the constants? Or should I use another approach?

推荐答案

答案是静态导入,您可以使用它来解决:

The answer is Static import, you can solve by using it:

导入静态mynamespace.MyClassConstants.*;

另请参见:

这篇关于导入的常量类字段的简称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆