字符转变量
<%
class Init
{
public String strInput;
Init(String strInput)
{
this.strInput = strInput;
}
}
class StrToVal
{
Init newInit = new Init("11234");
String getClassField(String fieldname,Class classInt) throws Exception
{
if (classInt.getField(fieldname)!=null)
return classInt.getField(fieldname).get(newInit).toString();
throw new Exception(classInt.getName()+"No Such Field"+ fieldname);
}
public String strGet() throws Exception
{
return getClassField("strInput",Init.class);
}
}
/*
String mystr="1234";
String my="mystr";
StrToVal str1 = new StrToVal();
String temp;
str1.newInit.strInput=my;
temp=str1.strGet();
out.println(temp);
out.println("<br>");
**/
%>
博客展示了字符转变量的代码实现。定义了 Init 类和 StrToVal 类,StrToVal 类中的 getClassField 方法用于获取类字段值,若字段存在则返回其值,不存在则抛出异常。还给出了部分调用代码示例。
567

被折叠的 条评论
为什么被折叠?



