Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OOP中两个类之间的交互_Java_Oop - Fatal编程技术网

Java OOP中两个类之间的交互

Java OOP中两个类之间的交互,java,oop,Java,Oop,我正在尝试用JAVA学习OOP。我想制作一个简单的基于OOP的控制台应用程序。整个应用程序大约有两类:汽车和车库 这是我的车库课: public class Garage { private int capacity; } 这是我的汽车级别: public class Car { private String type; private String color; public Auto(String type, String color){

我正在尝试用JAVA学习OOP。我想制作一个简单的基于OOP的控制台应用程序。整个应用程序大约有两类:汽车和车库

这是我的车库课:

public class Garage {
    private int capacity;


}
这是我的汽车级别:

public class Car {
    private String type;
    private String color;

    public Auto(String type, String color){
        this.color = color;
        this.type = type;
    }

    public String getColor(){
        return color;
    }

    public String getType(){
        return type;
    }

    public void Park(Garage garage){

    }

}
我不知道的是如何让它们相互作用。这意味着我不知道如何创建Park()方法。这个方法应该很简单,把车停在车库里,这样我以后就可以写下所有停在车库里的车

你需要一种方法把汽车存放在车库里。如果您提前知道车库的容量(比如2),您可以对车库内的车辆进行“硬编码”:

class Garage {
    Car slot1;
    Car slot2;
}

void Park(Garage g) {
    if (g.slot1 == null) {
        g.slot1 = this;
    } else if (g.slot2 == null) {
        g.slot2 = this;
    }
}
如果您想输出车库内的所有汽车,可以通过测试每个插槽是否为空(并且只打印非空插槽)来实现

这不是一个很好的解决方案:

  • 如果您事先不知道插槽的数量,您要么根本无法使用它,要么被迫创建大量插槽以覆盖最坏的情况
  • 如果将来车库的容量发生变化,则必须手动添加插槽3/4/。。。你的车库类,以及更新你的整个代码库,现在也考虑插槽3/4/。。无论车库在哪里使用
  • 也许你不在乎你的车停在哪个特定的位置,但你只是对你的车库里有什么车感兴趣
为此,我们有Java中的“集合”。它们允许我们在一个变量中存储任意数量的特定类型的对象。一个例子是一个例子。我将向您介绍如何使用它:

class Garage {
    LinkedList<Car> parkedCars;
}
class车库{
LinkedList停车场;
}
现在你必须

  • 初始化车库构造器中的驻车
  • 通过调用LinkedList.Add方法在Car.Park内停车
  • 一旦你解决了这两个问题,考虑一下如果你连续多次把同一辆车停在车里会发生什么。这是理想的结果吗?看看。

    你需要一种方法把汽车存放在车库里。如果您提前知道车库的容量(比如2),您可以对车库内的车辆进行“硬编码”:

    class Garage {
        Car slot1;
        Car slot2;
    }
    
    void Park(Garage g) {
        if (g.slot1 == null) {
            g.slot1 = this;
        } else if (g.slot2 == null) {
            g.slot2 = this;
        }
    }
    
    如果您想输出车库内的所有汽车,可以通过测试每个插槽是否为空(并且只打印非空插槽)来实现

    这不是一个很好的解决方案:

    • 如果您事先不知道插槽的数量,您要么根本无法使用它,要么被迫创建大量插槽以覆盖最坏的情况
    • 如果将来车库的容量发生变化,则必须手动添加插槽3/4/。。。你的车库类,以及更新你的整个代码库,现在也考虑插槽3/4/。。无论车库在哪里使用
    • 也许你不在乎你的车停在哪个特定的位置,但你只是对你的车库里有什么车感兴趣
    为此,我们有Java中的“集合”。它们允许我们在一个变量中存储任意数量的特定类型的对象。一个例子是一个例子。我将向您介绍如何使用它:

    class Garage {
        LinkedList<Car> parkedCars;
    }
    
    class车库{
    LinkedList停车场;
    }
    
    现在你必须

  • 初始化车库构造器中的驻车
  • 通过调用LinkedList.Add方法在Car.Park内停车

  • 一旦你解决了这两个问题,考虑一下如果你连续多次把同一辆车停在车里会发生什么。这是理想的结果吗?看看。

    在车库课程中,您可以添加一个列表来跟踪车库中的汽车

    private List<Car> carsParked;
    
    
    //just using arraylist as an example you could use any other of list or even a set if you so wish
    public Garage() {
    
       carsParked = new ArrayList<>();
    }
    
    在汽车类中的泊车方法中,执行以下操作:

    public void park(Garage g) {
    
          g.addCar(this);
    }
    

    在车库课程中,您可以添加一个列表来跟踪车库中的汽车

    private List<Car> carsParked;
    
    
    //just using arraylist as an example you could use any other of list or even a set if you so wish
    public Garage() {
    
       carsParked = new ArrayList<>();
    }
    
    在汽车类中的泊车方法中,执行以下操作:

    public void park(Garage g) {
    
          g.addCar(this);
    }
    

    就风格而言,你的方法应该以小写开头。就风格而言,你的方法应该以小写开头。