简体   繁体   English

有人可以用Java解释这个方法吗:

[英]Can someone please explain this method in Java:

This is in a class called a resource.这是在一个称为资源的类中。 I'm not sure what it does and need help understanding it.我不确定它的作用,需要帮助理解它。 trainingDTO has a UUID and an Item. trainingDTO 有一个 UUID 和一个项目。

if (this.toDoList.addItem(trainingDto.getItem())) {
          return Response.status(200).build();
          TrainingDTO returnDTO = new TrainingDTO();
          returnDTO.setItem(trainingDto.getItem());
          UUID uuid = toDoList.findUUIDByValue(trainingDto.getItem());
          returnDTO.setUUID(uuid.toString());
          return Response.ok(returnDTO).build();
        }

Explaining this block is a little difficult for two reasons.由于两个原因,解释这个块有点困难。 1.) you didn't give us much context: ie we don't have the method containing the block, or any other surrounding code. 1.) 你没有给我们太多的上下文:即我们没有包含块的方法,或任何其他周围的代码。 2.) this method has some odd code. 2.) 这个方法有一些奇怪的代码。 That being said, let's go through line by line:话虽如此,让我们一行一行地进行:

if (this.toDoList.addItem(trainingDto.getItem())) {

This if condition is a little complicated because addItem() obviously does more than just return a boolean.这个if条件有点复杂,因为addItem()显然不仅仅是返回一个布尔值。 Generally, this is considered bad form (methods should do just one thing, and adding to a list and returning a boolean are two things).通常,这被认为是不好的形式(方法应该只做一件事,添加到列表返回布尔值是两件事)。 But that's what it does: it adds (or tries to add) to toDoList , and returns a boolean.但这就是它所做的:它添加(或尝试添加)到toDoList ,并返回一个布尔值。

    return Response.status(200).build();

As written, this will always be the last line executed.正如所写,这将始终是执行的最后一行。 This return statement will always cause the function to return, and it means the lines below will not be executed.这个 return 语句总是会导致函数返回,这意味着下面的几行不会被执行。 But if it didn't, then the lines below would...但如果没有,那么下面的几行将......

    TrainingDTO returnDTO = new TrainingDTO();

Create a new object of type TrainingDTO创建一个TrainingDTO类型的新对象

    returnDTO.setItem(trainingDto.getItem());

Set a field of that object based on a field of an existing object (the same one referenced in the if condition)根据现有对象的字段设置该对象的字段(与if条件中引用的字段相同)

    UUID uuid = toDoList.findUUIDByValue(trainingDto.getItem());

Find the value of something called a UUID for the object from the if conditionif条件中为对象找到称为 UUID 的值

    returnDTO.setUUID(uuid.toString());

Use the UUID value above to set the UUID value for our new object使用上面的 UUID 值为我们的新对象设置 UUID 值

    return Response.ok(returnDTO).build();

Make a Response using the new object, and call its build() method, then return that result使用新对象做出Response ,并调用其build()方法,然后返回该结果

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM