JAVA myString['name'] = "我的值"; (就像在 PHP 中一样)

发布于 2024-08-15 18:02:42 字数 681 浏览 3 评论 0 原文

在 Php 中,我经常使用这个:

$conn_sets = array();
$conn_sets['login'] = "aaa";
$conn_sets['pass'] = "bbb";

How to do the same in JAVA 1.6。 我尝试这样做:

private method1() {    
    String[] mystring = new String[] {"login" => "aaa", "pass" => "bbb"};
}

但这给了我一个错误。 我想完成这项工作,因为我有一个错误列表声明,并且最好识别:

throw new MyException(myerrors['failed_login_error']);

比:

throw new MyException(myerrors[116]);

我知道我可以做一个新类,并抛出一个对象:

throw new MyException(ERROR_CONSTANTS.FAILED_LOGIN_ERROR);

但我更喜欢第一个(与我相同)在 PHP 中使用)。

那么,有什么想法吗?

In Php I really often use this one:

$conn_sets = array();
$conn_sets['login'] = "aaa";
$conn_sets['pass'] = "bbb";

How to do the same in JAVA 1.6.
I tried to do this:

private method1() {    
    String[] mystring = new String[] {"login" => "aaa", "pass" => "bbb"};
}

But it give's me an error.
I want to make this work, because I have an error lists declarations, and it is better to identify:

throw new MyException(myerrors['failed_login_error']);

than a:

throw new MyException(myerrors[116]);

I know I can do a new class, and throw an object:

throw new MyException(ERROR_CONSTANTS.FAILED_LOGIN_ERROR);

But I prefer the first one (the same as I use in Php).

So, any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不寐倦长更 2024-08-22 18:02:42

在 Java 中,您可能想要使用 地图接口,例如 HashMap

虽然,我想说使用 Enum 是实际上你应该在第二个(错误列表)示例中做什么。当您使用 Java 时,请忘记 PHP。在这种情况下,枚举要好得多,因为您需要一个定义明确的键列表。

In Java, you probably want to use the Map interface, such as a HashMap.

Although, I would say that using an Enum is actually what you should be doing in your second (list of errors) example. Forget about PHP when you're in Java. Enums are much better in this case because you want a well-defined list of keys.

黑凤梨 2024-08-22 18:02:42

您确实应该使用 Properties (或更好的是 ResourceBundle 来抽象属性文件)对于这种特殊情况。

这是关于用法的教程

这是一种更好的方法,因为您可以国际化(I18N)消息(如果您愿意),并且您可以在文本文件中而不是在代码中指定它们(消息在文本中比在代码中好得多,因此您可以更新它们而无需重建)。

You really should be using Properties (or better yet a ResourceBundle to abstract the properties file) for this particular case.

Here is a tutorial on the usage.

This is a much better way as you can internationalize (I18N) the messages (if you want) and you can specify them in text files rather than inside the code (messages are much better in text than in code so you can update them without having to rebuild).

墨落成白 2024-08-22 18:02:42

您不想使用 HashMapProperties 对象来存储广泛命名的参数。您确实想要考虑面向对象并使用类来封装帐户的数据并表达对象在现实世界中真正代表的内容:

String username = "aaa";
String password = "bbb";
Account acc = new Account(username,password);
if (!tryLogin(acc)) {
 throw new LoginFailedException(account);
}

这样,捕获 LoginFailedException 的客户端就可以使用信息并使用具有良好名称的静态类型方法,例如通过调用 loginFailedException.getUsername() 检索用户名。

You don't want to use a HashMap or a Properties object to store wildly named parameters. You really want to think object-oriented and use a class to encapsulate the data of an account and to express what an object really represents in the real world:

String username = "aaa";
String password = "bbb";
Account acc = new Account(username,password);
if (!tryLogin(acc)) {
 throw new LoginFailedException(account);
}

That way, clients who catch the LoginFailedException can make use of the information and use statically typed methods with good names to, for example, retrieve the username by calling loginFailedException.getUsername().

瞳孔里扚悲伤 2024-08-22 18:02:42

您可以使用双括号模式:

Map<String, String> map = new HashMap<String, String>() {{
    put( "login", "aaa" );
    put( "pass", "bbb" );
}};

...以及您的其他示例:

throw new MyException( myErrors.get( "failed_login_error" ) );

You could use the double brace pattern:

Map<String, String> map = new HashMap<String, String>() {{
    put( "login", "aaa" );
    put( "pass", "bbb" );
}};

... and your other example:

throw new MyException( myErrors.get( "failed_login_error" ) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文