专业使用抽象类进行翻译 [英] professional usage of abstract class for translation
问题描述
下面的代码没有给我答案,我想,我不知道在哪里的问题?
FR是EN的翻译(完全像.properties文件)
i如果
i想要访问fr.java或en的hello变量,则要从FR.java文件中读取翻译。 java从index.jsp页面。但我写的代码给了我从Lang.java的值
the code below is not giving me the answer i want, i don't know where is the problem? FR is the translation of EN (exactly like .properties file) i want to read the translation from the FR.java file if i want to reach the hello variable of fr.java or en.java from the index.jsp page. but code i wrote gives me the value from Lang.java
String language = "FR";
条件是在.jsp文件中
jdk 1.4给我这个错误:Error 23,23):variable lang可能没有初始化
the condition is in the .jsp file jdk 1.4 gives me this error :Error(23,23): variable lang might not have been initialized
任何机构都可以帮助,代码请求?
any body can help, code pleas?
文件名Lang.java
file name Lang.java
package mypackage;
abstract public class Lang {
public String hello= "home page";
}
filename EN.java
filename EN.java
package mypackage;
public class EN extends Lang {
public String hello = "hello";
}
filename FR.java
filename FR.java
package mypackage;
public class FR extends Lang {
public String hello = "bonjour";
}
文件名:index.jsp
file name : index.jsp
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="mypackage.Lang" %>
<%@ page import="mypackage.FR" %>
<%@ page import="mypackage.EN" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>language</title>
</head>
<body>
<%
String language = "EN";
Lang lang;
if (language.equals("EN")){
lang = new EN();
}
else if (language.equals("FR")){
lang = new FR();
}
%>
<%
out.print(lang.hello);
%>
</body>
</html>
推荐答案
<%
String language = "EN";
Lang lang;
if (language.equals("EN")){
lang = new EN();
}
else if (language.equals("FR")){
lang = new FR();
}
%>
这里可以是语言保持未初始化的状态,因此您需要初始化
Here it can be the case where language stays un initialized so you need to initialize it
说
Lang lang = null;//or any default value
并且初始化局部变量是必须的
And to initialize local variable is compulsary
不理解这种条件的重要性,你将EN
分配给语言,那么条件的需要是什么?
I don't understand the importance of this condition here you are assigning "EN"
to language then what is the need of condition?
这篇关于专业使用抽象类进行翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!