簡體   English   中英

Java:代碼理解

[英]Java: Code Understanding

我是JAVA的新手,但是我了解Objective-C。 我必須編寫服務器端的自定義代碼,而下面的代碼有麻煩:

/**
 * This example will show a user how to write a custom code method
 * with two parameters that updates the specified object in their schema
 * when given a unique ID and a `year` field on which to update.
 */

public class UpdateObject implements CustomCodeMethod {

  @Override
  public String getMethodName() {
    return "CRUD_Update";
  }

  @Override
  public List<String> getParams() {
    return Arrays.asList("car_ID","year");
  }

  @Override
  public ResponseToProcess execute(ProcessedAPIRequest request, SDKServiceProvider serviceProvider) {
    String carID = "";
    String year  = "";

    LoggerService logger = serviceProvider.getLoggerService(UpdateObject.class);
    logger.debug(request.getBody());
    Map<String, String> errMap = new HashMap<String, String>();

    /* The following try/catch block shows how to properly fetch parameters for PUT/POST operations
     * from the JSON request body
     */

    JSONParser parser = new JSONParser();
    try {
      Object obj = parser.parse(request.getBody());
      JSONObject jsonObject = (JSONObject) obj;

      // Fetch the values passed in by the user from the body of JSON
      carID = (String) jsonObject.get("car_ID");
      year = (String) jsonObject.get("year");
      //Q1:  This is assigning the values to  fields in the fetched Object?

    } catch (ParseException pe) {
      logger.error(pe.getMessage(), pe);
      return Util.badRequestResponse(errMap, pe.getMessage());
    }

    if (Util.hasNulls(year, carID)){
      return Util.badRequestResponse(errMap);
    }

    //Q2:  Is this creating a new HashMap?  If so, why is there a need?
    Map<String, SMValue> feedback = new HashMap<String, SMValue>();
    //Q3:  This is taking the key "updated year" and assigning a value (year)?  Why?
    feedback.put("updated year", new SMInt(Long.parseLong(year)));

    DataService ds = serviceProvider.getDataService();
    List<SMUpdate> update = new ArrayList<SMUpdate>();

    /* Create the changes in the form of an Update that you'd like to apply to the object
     * In this case I want to make changes to year by overriding existing values with user input
     */
    update.add(new SMSet("year", new SMInt(Long.parseLong(year))));

    SMObject result;
    try {
      // Remember that the primary key in this car schema is `car_id`

      //Q4: If the Object is updated earlier with update.add... What is the code below doing?
      result = ds.updateObject("car", new SMString(carID), update);

      //Q5: What's the need for the code below?
      feedback.put("updated object", result);

    } catch (InvalidSchemaException ise) {
      return Util.internalErrorResponse("invalid_schema", ise, errMap);  // http 500 - internal server error
    } catch (DatastoreException dse) {
      return Util.internalErrorResponse("datastore_exception", dse, errMap);  // http 500 - internal server error
    }

    return new ResponseToProcess(HttpURLConnection.HTTP_OK, feedback);
  }

}

問題1:以下代碼將值分配給獲取的對象中的字段?

carID = (String) jsonObject.get("car_ID");
year = (String) jsonObject.get("year");

問題2:這是否在創建新的HashMap? 如果是這樣,為什么需要?

Map<String, SMValue> feedback = new HashMap<String, SMValue>();

問題3:這是采用鍵“更新的年份”並分配一個值(年份)嗎? 為什么?

feedback.put("updated year", new SMInt(Long.parseLong(year)));

Q4:如果該對象較早使用update.add更新...下面的代碼在做什么?

result = ds.updateObject("car", new SMString(carID), update);

Q5:下面的代碼在做什么?

feedback.put("updated object", result);

原始碼

SMSet

SMInt

問題1:他們從讀取的JSON對象中讀取數據,並將字段car_ID和year的值存儲在兩個具有相同名稱的局部變量中。

問題2:是的。 反饋似乎是將以JSON形式發送回客戶端的地圖

問題3:它將讀取的值存儲到新創建的哈希表“ feedback”中的局部變量“ year”(如前所述)中

Q4:不確定,我假設ds對象是某種數據庫。 如果是這樣,則它看起來將存儲在哈希圖中的更新值“更新”中並將其推送到數據庫中。

問題5:它將“結果”對象存儲在反饋哈希圖中的鍵“已更新對象”下。

希望這可以幫助 :)

Q1不,它似乎沒有在設置類成員變量,而是在execute()方法本地設置了變量。 方法返回后,GC將清除這些本地變量。 好吧,不是真的,但是它們現在要接受GC的檢驗,但這確實變得技術性了。

Q2是的,您正在創建一個HashMap並將其引用放入Map Map是一個接口,在Java中引用這樣的東西是一種好習慣。 這樣,您就不會將代碼綁定到特定的實現。 我相信在Objective-C中它們被稱為原型???

Q3我不確定他們為什么這樣做。 我假設在代碼中的某處使用了feedback Map ,然后將該值取回。 將地圖視為NSDictionary 看起來“ year”是一個String ,因此他們使用Long.parseLong()進行轉換。 不確定SMInt是什么...從名稱上看,它看起來像代表“ small int”的自定義類????

Q4我不知道什么是DataService ,但是我不得不猜測它的某種服務來讀取/寫入數據? 從該方法中,我猜想它會調用服務來更新您剛剛更改的值。

問題5同樣, feedback是一個Map ...將result放入該Map的“更新的對象”鍵中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM