我想要做的是使用第二種方法來創建一個模式,這樣在第一種方法中我只需要用引數撰寫第二種方法名稱,以便通過傳遞鍵名從回應正文中提取不同的值作為引數。
第二種方法的回應顯示錯誤“無法決議符號'回應'”
public class BDDStyledMethod {
public static void GetActivityById(){
Response response=Authentication.Creds("www.randomurl.com");
System.out.println("The extracted thing is: " bodyResponse("name"));
}
public static String bodyResponse(String exe){
JsonPath jsonPathEvaluator = response.jsonPath();
String bodyExample = jsonPathEvaluator.get(exe).toString();
return bodyExample;
}
uj5u.com熱心網友回復:
旁白:這是一個特定于 java 的問題,而不是面向物件的問題。
您問題的癥結在于Java范圍。變數response在GetActivityById方法中定義。該方法有 a{和 a }。那就是變數的作用域。變數的作用域意味著,該變數僅是可見的 - 意思是,它僅在定義之后“存在” [ 1],[2]直到定義它的范圍(大括號)結束。
但是,如果您從methodA呼叫方法methodB,則methodB將具有自己的范圍,該范圍與呼叫它的方法methodA的范圍是分開的。
該方法bodyResponse只能訪問在其范圍內定義的變數或作為引數傳遞給它的變數。因此,要將變數從一種方法共享到另一種方法,一種方法是將其作為變數傳遞。您已經使用exe變數執行了此操作。因此,對response變數執行相同的操作以修復此錯誤:
public static void GetActivityById(){
Response response=Authentication.Creds("www.randomurl.com");
System.out.println("The extracted thing is: " bodyResponse("name", response));//note I added *response*
}
public static String bodyResponse(String exe, Response response){//note I added *response*
JsonPath jsonPathEvaluator = response.jsonPath();
String bodyExample = jsonPathEvaluator.get(exe).toString();
return bodyExample;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/497377.html
下一篇:如何從變數中洗掉值
