我试图将硬件组件添加到包含对象阵列的PCB上,但无法将对象传递给该阵列。这是我的第一个Java项目,所以在这里是新手。请帮忙!

public class PCB {
    private Collection<HardwareComponent> hwComponents = new Vector<HardwareComponent>();
    private Collection<CircuitPath> connections = new Vector<CircuitPath>();

    public void placeComponent(<HardwareComponent> hw) {
        hwComponents.add(hw);
    }


而我的主

 public class Configurator {

    public static void main(String[] args) {

        PCB pcb = new PCB();

        HardwareComponent c1 = new Capacitor("cap1", 0.55f);
        HardwareComponent c2 = new Capacitor("cap2", 0.22f);
        HardwareComponent c3 = new Capacitor("cap3", 0.50f);
        HardwareComponent c4 = new Capacitor("cap4", 0.75f);
        HardwareComponent r1 = new Resistor("res1", 0.14f);
        HardwareComponent r2 = new Resistor("res2", 0.18f);
        HardwareComponent r3 = new Resistor("res3", 0.17f);
        HardwareComponent r4 = new Resistor("res4", 0.10f);

        pcb.placeComponent(c1);


我收到此错误:
PCB类型中的方法placeComponent()不适用于自变量(HardwareComponent)

不知道从哪里开始,我是否需要为类PCB创建一个单独的构造函数,或者如何将HardwareComponent传递给placeComponent方法?

先感谢您

最佳答案

方法签名有误
public void placeComponent(<HardwareComponent> hw)
试试看
public void placeComponent(HardwareComponent hw)

关于java - PCB类型中的方法placeComponent()不适用于自变量(HardwareComponent),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60993541/

10-11 17:46