Java:代码理解



我是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);
  }
}

Q1:下面的代码是否将值分配给获取对象中的字段?

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

Q2:这是在创建一个新的HashMap吗? 如果是这样,为什么有需要?

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

Q3:这是取关键的"更新年份"并分配一个值(年份)? 为什么?

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

问题 4:如果对象之前使用 update.add 更新... 下面的代码在做什么?

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

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

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

原始代码

短信

斯敏特

问题 1:它们从获取的 JSON 对象中读取,并将字段 car_ID 和 year 的值存储在两个具有相同名称的局部变量中。

Q2: 是的。反馈似乎是一个地图,将作为JSON发送回客户端

Q3:它将读取到局部变量"year"(如前所述)的值存储在新创建的哈希图"反馈"中

Q4:不确定,我假设 ds 对象是某种数据库。如果是这样,它看起来像是获取存储在哈希图"更新"中的更新值并将其推送到数据库。

Q5:它将"结果"对象存储在反馈哈希图中的键"更新对象"下。

希望这对:)有所帮助

Q1不,它似乎不是在设置类成员变量,而是在 execute() 方法的本地变量。 一旦方法返回,GC 就会清理这些本地变量。 嗯,不是真的,但他们现在受 GC 的约束,但这变得非常技术化。

第 2 季度是的,您正在创建一个HashMap并将其引用放入Map中。 Map是一个接口,在 Java 中引用这样的东西是一种很好的做法。 这样,您就不会将代码绑定到特定的实现。 我相信在Objective-C中,它们被称为原型???

第三季度我不确定他们为什么要这样做。 我假设在代码中的某个地方使用了feedback Map,并且该值被提取回来。 将地图视为NSDictionary。 看起来"年"是一个String,所以他们用Long.parseLong()来转换它。 不知道SMInt是什么...从名称上看,它看起来像一个表示"小整数"的自定义类???

第 4 季度我不知道DataService是什么,但我不得不猜测它是读取/写入数据的某种服务??? 从该方法中,我猜它调用服务来更新您刚刚更改的值。

问5同样,feedback是一个Map...它将result放在该地图的"更新对象"键中。

最新更新