用戶輸入一個表達式。假設用戶輸入以下內容作爲輸入:如何創建類聲明對象,然後分配值
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