我想问一下,如果我们应该外化toString()实现中使用的字符串文字。让我举个例子: -
假设这是我的toString()实现: -
@Override
public String toString() {
return "First Name: " + firstName + "," + "Last Name: " + lastName;
}
在这里,我们应该外化“名字:”& “姓氏:”或不?
另一种情况是验证字符串。如果我创建名字对象,名字和姓氏不能为空。然后我在构造函数中加入以下检查: -
if(firstName == null) {
throw new NullPointerException("firstName is null");
}
我们应该将“firstName为null”外化吗?
由于
答案 0 :(得分:4)
如果您希望允许调试消息国际化,您真的只需要这样做,因为toString
不应该真正显示给用户
答案 1 :(得分:1)
您可能还想看看这个: http://tika.apache.org/
因此,当tika为您提供字符串的语言时,您应该编写如下的Java方法:
enum languageToTransformString //create however many languages you want
public String toString(String lang)
{
languageToTransformString = lang;
switch languageToTransformString:
case german:
firstNameText = "deutchName: ";
lastNameText = "deutchLastName: ";
return new String(firstnameText + firstName + lastNameText + lastName);
break;
case eng:
//do stuff
firstNameText = "engName: ";
lastNameText = "engLastName: ";
return new String(firstNameText + firstName + lastNameText + lastName);
break;
default:
return new String("We don't support your language. Sorry.");
}