Java – pass enum member to constructor: “actual and formal parameter lists are different in length”

I'm confused. I think what I do is very stupid. I just can't see it!

Central heating control:

Radiator class and radiatorsize class Radiator has a size instance variable, which is class radiatorsize I have XSmall,... XLarge for radiator size; Defined as a public enumeration in radiatorsize

So, I was thinking, create a new radiator and give it a medium size

size = new RadiatorSize(RadiatorSize.Size.MEDIUM);

NetBeans told me this:

...\CentralHeating\src\hardwaremodel\Radiator.java:17:
error: constructor RadiatorSize in class RadiatorSize cannot be
applied to given types;

size = new RadiatorSize(RadiatorSize.Size.MEDIUM);
required: no arguments
found: Size
reason: actual and formal argument lists differ in length
1 error

This is an illegal course

/** Radiator class **/
package hardwaremodel;

import units_constants.RadiatorSize;

public class Radiator {
    private int boilerSequence;
    private RadiatorSize size;
    private double volume; //cubic centimetres

public void Radiator(int blrSqnc) {
    boilerSequence = blrSqnc;
    size = new RadiatorSize(RadiatorSize.Size.MEDIUM); //ERROR HERE
    }
}


/** RadiatorSize class **/
package units_constants;

public class RadiatorSize {
    public enum Size {XSMALL,SMALL,MEDIUM,LARGE,XLARGE};

    private Size size;

    public void RadiatorSize(Size sz) {
        size = sz;
    }
}

Now, NetBeans has given me a hint that it suggests in units_ constants. Create a constructor radiatorsize (units_constants. Radiatorsize. Size) in radiatorsize; When I agree, it will do this:

public RadiatorSize(Size size) {
    throw new UnsupportedOperationException("Not yet implemented");
}

Now there is no little red circle in the editor, but!

I'm confused

Solution

You are trying to use it as a constructor:

public void RadiatorSize(Size sz) {
        size = sz;
}

Because this is not a constructor, but a void method, your new radiatorsize (size. Medium); Try calling a standard constructor without parameters Knowing this, the error message given is completely reasonable;)

Just get rid of the void return type and it should work properly

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>