用户输入一个表达式。假设用户输入以下内容作为输入:如何创建类声明对象,然后分配值
new y java.util.ArrayList int:5
我已成功标记字符串并将其存储到我的字符串数组的不同位置。接下来我想要做的是我应该检查索引上的内容,并根据上面提到的反射中提到的操作来执行操作。我坚持如何去做。这里是我到目前为止的代码
public static void handling_input()
{
System.out.println("Welcome To Java Command Prompt: ");
aLine = null;
try
{
System.out.println("Enter The Command Line Expression: ") ;
keyboard = new BufferedReader(new InputStreamReader(System.in));
aLine = keyboard.readLine();
st = new StringTokenizer(aLine);
dt = new StringTokenizer(aLine);
}
catch (IOException ex)
{
System.out.println("Error reading input!");
}
}
public static void storing_tokens()
{
int counter =0;
while(st.hasMoreTokens())
{
counter++;
st.nextToken();
}
int i=0;
expression_keeper= new String[counter];
do
{
expression_keeper[i] = dt.nextToken().toString();
i++;
}while(dt.hasMoreTokens());
}
public static void token_classification()
{
for(int i=0; i<expression_keeper.length; i++)
{
if(expression_keeper[0].equalsIgnoreCase("new"))
{
}
else
if(expression_keeper[0].equalsIgnoreCase("call"))
{
}
else
if(expression_keeper[0].equalsIgnoreCase("print"))
{
}
else
{
System.out.println("Invalid Script!");
}
}
}
}
这里面,如果条件:
if(expression_keeper[0].equalsIgnoreCase("new"))
{
}
我想创建指定的类,它的对象,并赋值提到的修饰!
[在Java中使用反射来创建新参考变量类型设置为新实例类名称的实例?](http://stackoverflow.com/questions/2215843/using-reflection-in-java -to-create-a-new-instance-with-the-reference-variable-ty) – 2013-03-04 17:48:23
@LuiggiMendoza,这正是初学者应该寻找的! – svz 2013-03-04 17:53:04