【问题标题】:Access method from another class No static?从另一个类访问方法没有静态?
【发布时间】:2016-09-22 11:41:03
【问题描述】:

由于静态方法和变量,我的代码中有很多问题。 我是编程新手,我注意到当我在类中使用静态时,我可以在任何我想要的地方调用它。 下面的链接是我之前的问题(如果你需要,这就是为什么尝试不使用静态),有人告诉我删除解决我部分问题的方法的静态。

PS:英语是我的第三语言,所以它可能很糟糕。 但是请帮帮我,我卡住了

Get random object from arraylist specific variable

所以我的问题:

我有一个构造函数来创建汽车,参数填充了函数。 但是由于它尚未创建的对象我无法获得方法。 有没有办法我不能打电话给他们? 这是我的代码:
现在Cars.getPosition() 带有红色下划线,它告诉我将其设为静态。

来自创建汽车的车库类:

public void addCar() {
    Cars car = new Cars(Cars.getID(), Cars.askCarID(), Cars.getPosition(), Attendant.askForAtt(), System.currentTimeMillis());
    myGarage.add(car);
    if(!(car.getAssignedTo()).equals(null)){
        car.getAssignedTo().setAssign(car);
        car.getAssignedTo().setAvailable(false);
    }
}

来自汽车类:

private static void createCarsID() {

    for (int x = 0; x < Garage.getCarsCapacity(); x++) {
        String tempCarID = ("CR" + (x + 1));
        tempArray2.add(tempCarID);
    }
}

public static String getID() {

    createCarsID();
    String tempID = null;
    String tempPos = null;
    for (int x = 0; x < Garage.getCarsCapacity(); x++) {
        if (tempArray2.get(x) != null) {
            tempID = tempArray2.get(x);
            tempPos = tempArray2.get(x);
            tempArray2.remove(tempArray2.get(x));
            getPos(tempPos);
            //tempArray2.get(x) = null;
            break;
        }
    }
    return tempID;
}

public void getPos(String IdToPos) {
    String strPos = IdToPos.substring(2);
    int pos = Integer.parseInt(strPos);
    position = "GR" + pos;

}

【问题讨论】:

  • 您的 getter 和 setter 不应该是静态的,它们用于获取或设置 Car 对象(实例)上的数据,而不是 Car 类上的数据。如果您想流线或简化您的汽车创建过程,请考虑使用 Builder 模式。如果您想在创建类时提供一些随机数据,请将其移到 Car 类之外,例如创建一个 CarDataProvider 类并在其中添加这些静态函数。

标签: java constructor static-methods


【解决方案1】:

这只是您可以做到的方式之一。新车的信息取自键盘,但不一定是这样的。

车库.java

package yourPackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Víctor Bernabé Pérez
 */
public class Garage {

    private final Scanner scanInt = new Scanner(System.in);
    private final InputStreamReader inStream = new InputStreamReader(System.in);
    private final BufferedReader scanText = new BufferedReader(inStream);
    private final ArrayList<Car> cars;

    public Garage() {
        cars = new ArrayList<>();
    }

    public void addCar() {
        try {
            System.out.print("id");
            int id = scanInt.nextInt();
            System.out.print("carId");
            int carId = scanInt.nextInt();
            System.out.print("position");
            int position = scanInt.nextInt();
            long timeMillis = System.currentTimeMillis();
            System.out.print("Attendant");
            String attendant = new String();
            try {
                attendant = scanText.readLine();
            } catch (IOException ex) {
                Logger.getLogger(Garage.class.getName()).log(Level.SEVERE, null, ex);
            }
            boolean free;
            String available = scanText.readLine();
            if (available.equals("s")) {
                free = true;
            } else {
                free = false;
            }

            cars.add(new Car(id, carId, position, attendant, timeMillis, free));
        } catch (IOException ex) {
            Logger.getLogger(Garage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

汽车.java

package yourPackage;

/**
 *
 * @author Víctor Bernabe Pérez
 */
public class Car {

    private int id, carId, position;
    private long timeMillis;
    private String attendant;
    private boolean available;

    public Car(){
        //Nothing, just default constructor
    }

    public Car(int id, int carId, int position, String attendant, long timeMillis, boolean available){
        this.id = id;
        this.carId = carId;
        this.position = position;
        this.attendant = attendant;
        this.timeMillis = timeMillis;
        this.available = available;
    } 
}

【讨论】:

  • 我不能它必须是像 CR1 或 Pos1 这样的字符串。
  • 系统应该是给予它的系统
  • 您发布了部分代码,所以我看不到您的类的字段或主要逻辑。但是您可以解决它,只需停止尝试创建一个包含必须从该对象本身返回的信息的对象。如果您发布所有代码,我可以为您提供确切的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多