简体   繁体   English

更新对象中的字段值

[英]Update field value in object

I need to update a fields value inside one of my object, just update the String. 我需要更新一个对象中的字段值,只需更新字符串即可。 The object is inside of an array. 该对象在数组内部。 But I will target the object by typing in the objects "regNum" in the parameter. 但是我将通过在参数中输入对象“ regNum”来定位对象。

This is what I tried, I really don't know how to use the set() method when I need to enter the list and the objects specific value. 这是我尝试过的方法,当我需要输入列表和对象特定值时,我真的不知道如何使用set()方法。

public boolean doesNotWork( String regNumInput ){
    for(int i = 0; i < meterList.size(); i++){
        if(regNumInput == meterList.get(i).getRegNum()){
            meterList.set(meterList.get(i).getWorkOrNot(), new String ("No"));
        }
    }
    return true;
}

This Is the whole MeterArchive class that stores the meters and have some methods to it. 这是整个MeterArchive类,用于存储计量表并具有一些方法。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MeterArchive
{
// instance variables - replace the example below with your own
ArrayList<Meter> meterList = new ArrayList<Meter>();

public void createClocks(){
    Clock clockOne = new Clock("KH001", "Yes", "ClassRoom005", 0.0);
    meterList.add(clockOne);
    Clock clockTwo = new Clock("KH002", "Yes", "ClassRoom006", 0.0);
    meterList.add(clockTwo);
}

public boolean doesNotWork( String regNumInput ){
    for(int i = 0; i < meterList.size(); i++){
        if(regNumInput == meterList.get(i).getRegNum()){
            meterList.set(meterList.get(i).getWorkOrNot(), new String ("No"));
        }
    }
    return true;
}

public void showAllMeter(){
    for(Meter meter : meterList){
        System.out.println(meter);
    }
}
}

This is the clock class that has specific clock values that you can add. 这是具有可添加的特定时钟值的时钟类。

public class Clock extends Meter
{

/**
 * Constructor for objects of class Clock
 */
public Clock(String regNum, String workOrNot, String location, double minTime)
{
    // initialise instance variables
    super(regNum, workOrNot, location);
    setMinTime(minTime);

}

//MINNIMUM TIME
public void setMinTime(double minTime){
    this.minTime = minTime;
}

public double getMinTime(){
    return minTime;
}

//EQUALS METHOD --- NOT SURE WHAT IT SHOULD DO... YET!
public boolean equals (Clock other){
    return location.equals(other.location);
}

public String toString(){
    String retur = super.toString() + "regNum: " + regNum +
                                      "Does it work: " + workOrNot +
                                      "Location: " + location +
                                      "Min time value: " + minTime;
    return retur;
}
}

This is the super class that has more general input for the different meters. 这是超类,具有针对不同仪表的更多常规输入。

public class Meter
{

public String regNum;
public String workOrNot;
public String location;


/**
 * Constructor for objects of class Clock
 */
public Meter(String regNum, String workOrNot, String location)
{
    // initialise instance variables
    setRegNum(regNum);
    setWorkOrNot(workOrNot);
    setLocation(location);
}

//REGISTRATION NUMBER
public void setRegNum(String regNum){
    this.regNum = regNum;
}

public String getRegNum(){
    return regNum;
}

//WORK OR NOT
public void setWorkOrNot(String workOrNot){
    this.workOrNot = workOrNot;
}

public String getWorkOrNot(){
    return workOrNot;
}

//LOCATION
public void setLocation(String location){
    this.location = location;
}

public String getLocation(){
    return location;
}
}

So in the MeterArchive class I want to change the field value "workOrNot" from whatever it is (most likely "Yes") to "No". 因此,在MeterArchive类中,我想将字段值“ workOrNot”从它的值(很可能是“ Yes”)更改为“ No”。 I found out that set() is usually the way to go, but in this program I want the user to add the specific "regNum" and then the method will change to String inside the "workOrNot" field to "No". 我发现通常使用set()方法,但是在此程序中,我希望用户添加特定的“ regNum”,然后该方法将在“ workOrNot”字段内的String更改为“ No”。 As I said earlier I dont know how to target the specific field inside the object. 如前所述,我不知道如何定位对象内的特定字段。 Can someone explain how to do this? 有人可以解释如何做吗?

You need to use setter method setWorkOrNot() to update field workOrNot on the desired Meter object. 您需要使用setter方法setWorkOrNot()更新所需的Meter对象上的字段workOrNot

Use the below code: 使用以下代码:

public boolean doesNotWork( String regNumInput ){
    for(int i = 0; i < meterList.size(); i++){
        if(regNumInput.equals(meterList.get(i).getRegNum())){
            meterList.get(i).setWorkOrNot("No");
        }
    }
    return true;
}

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

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