【问题标题】:Edit Values of an object in ObjectArrayList JAVA在 ObjectArrayList JAVA 中编辑对象的值
【发布时间】:2020-07-08 08:37:06
【问题描述】:

我正在尝试在名为 lift 的数组列表中编辑 LiftObject.java 中的 liftobject 值。 我正在尝试使用 setDirection() 将此值从 0 更改为 22(应该是 -1 但我只是在测试)。

LiftObject.java

package system;

import java.io.Serializable;

public class LiftObject implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    int capacity;
    int direction;
    int currentFloor;
    int LiftNum;
    int id;

    public LiftObject(int capacity, int direction, int currentFloor, int LiftNum){
        this.capacity = capacity;
        this.direction = direction;
        this.currentFloor = currentFloor;
        this.LiftNum = LiftNum;
    }

    public int getCapacity(){return this.capacity;}
    public int getDirection(){return this.direction;}
    public int getCurrentFloor(){return this.currentFloor;}
    public int getLiftNum(){return this.LiftNum;}

    public int setCapacity(int capacity){return this.capacity;}
    public int setDirection(int direction){return this.direction;}
    public int setCurrentFloor(int currentFloor){return this.currentFloor;}
    public int setLiftNum(int LiftNum){return this.LiftNum;}


}

然后是主文件,TestSystem.java

package system;

public class TestSystem {
    static int TopFloor = 20;
    static int Capacity = 8;
    int W_PersCount = 0;
    int A_PersCount = 0;
    int T_PersCount = 0;
    public static ObjectArrayList lift = new ObjectArrayList();
    public static ObjectArrayList wPers = new ObjectArrayList();
    public static ObjectArrayList tPers = new ObjectArrayList();
    public static ObjectArrayList aPers = new ObjectArrayList();

    public void DirectLift() {
        System.out.println("Direct Lift Start.");
        //The lift only changes direction when it hits the top or bottom
        int CF =((LiftObject) TestSystem.lift.get(0)).getCurrentFloor();
        System.out.println("Floor: ");
        System.out.println(CF);
        if( CF == 0) {
            ((LiftObject) TestSystem.lift.get(0)).setDirection(+1);
            System.out.println(((LiftObject) TestSystem.lift.get(0)).getDirection());
        }else if(CF == TopFloor){
            ((LiftObject) TestSystem.lift.get(0)).setDirection(-1);
            System.out.println(((LiftObject) TestSystem.lift.get(0)).getDirection());
        }else {
            System.out.println("Continue");
        }

    }


    public void LiftGeneration(int capacity, int direction, int currentFloor, int LiftNum) {
        LiftObject obj = new LiftObject(capacity,direction,currentFloor,LiftNum);
        obj.capacity = capacity;
        obj.direction = direction;
        obj.currentFloor = currentFloor;
        obj.LiftNum = LiftNum;


        lift.add(obj);
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getCapacity());
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getDirection());
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getCurrentFloor());
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getLiftNum());
    }

    public void System() {
        //The lift only changes direction when it hits the top or bottom
        int CF =((LiftObject) TestSystem.lift.get(0)).getCurrentFloor();
        System.out.println("Floor: ");
        System.out.println(CF);
        System.out.println("Direct Lift Start.");
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getCapacity());
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getDirection());
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getCurrentFloor());
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getLiftNum());
        ((LiftObject)TestSystem.lift.get(0)).setDirection(22);
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getDirection());

    }

    public static void main(String[] args) {
        System.out.println("Start.");
        TestSystem myObj = new TestSystem(); // Create an object of MyClass
        myObj.LiftGeneration(Capacity, 0, 0, 1);
        System.out.println("Works here");
        myObj.System();

    }

问题是 当我尝试将方向从 0 更改为 -1 时没有发生任何变化 任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 你的二传手完全错了。他们应该设置值而不是返回值。大多数 IDE 都可以选择为您生成 getter/setter。

标签: java oop arraylist


【解决方案1】:

你的问题在这里:

 public int setDirection(int direction){return this.direction;}

应该是:

 public void setDirection(int direction){this.direction = direction;}

你所有的 setter 都和你的 getter 一样写。当您编写 setter 时,您不会返回值;你设置它。您需要将所有其他设置器调整为类似于此处的设置。否则,您也会遇到问题。

【讨论】:

    猜你喜欢
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2021-03-05
    • 2019-01-14
    • 2019-03-27
    • 1970-01-01
    相关资源
    最近更新 更多