如何处理Java中的空参数?

如何处理Java中的空参数?,java,Java,我有一个简单的程序,它读取参数并作为结果输出它们 例如,如果我使用: Rectangle r1 = new Rectangle(1.0, 2.0, "RED", false); System.out.println(r1); 应返回: 1.0 x 2.0, color: RED 但如果我输入以下参数会怎样: Rectangle r8 = new Rectangle(); System.out.println(r8); 当使用上述参数时,我的程序如何输出以下默认结果 1.0 x 1.0, f

我有一个简单的程序,它读取参数并作为结果输出它们

例如,如果我使用:

Rectangle r1 = new Rectangle(1.0, 2.0, "RED", false);
System.out.println(r1);
应返回:

1.0 x 2.0, color: RED
但如果我输入以下参数会怎样:

Rectangle r8 = new Rectangle();
System.out.println(r8);
当使用上述参数时,我的程序如何输出以下默认结果

1.0 x 1.0, filled with RED
以下是我代码的一部分:

public class Rectangle extends Shape {

    protected double width;
    protected double length;

    public Rectangle() {
        super();
    }

    public Rectangle(double width, double length) {
        super();
        this.width = width;
        this.length = length;
    }

    public Rectangle(double width, double length, String color, boolean filled) {
        super(color, filled);
        this.width = width;
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }   

    public double getArea() {
        return (width * length);
    }

    public double getPerimeter() {
        return (2 * (width + length));
    }

    public String toString() {
        if (super.isFilled()) {
        return width + " x " + length + ", " + "filled with RED";
        } else {
        return width + " x " + length + ", " + "color: RED";
        }
    }
}

我尝试使用if width=0{}表示宽度,但由于它是一个基本类型,因此无法工作……有人能告诉我,如果使用空参数,如何打印默认值吗?谢谢你的帮助

您可以在无参数构造函数中设置默认值:

public Rectangle() {
    super("RED", true);
    this.width = 1.0;
    this.length = 1.0;
}
也可以在成员声明中指定默认值:

protected double width = 1.0;
protected double length = 1.0;
并且具有类似的声明,形状成员具有默认值

或者,您可以根据Seelenvirtuose的建议,从无参数构造函数调用不同的构造函数:

public Rectangle() {
    this(1.0, 1.0, "RED", true);
}

您可以在无参数构造函数中设置默认值:

public Rectangle() {
    super("RED", true);
    this.width = 1.0;
    this.length = 1.0;
}
也可以在成员声明中指定默认值:

protected double width = 1.0;
protected double length = 1.0;
并且具有类似的声明,形状成员具有默认值

或者,您可以根据Seelenvirtuose的建议,从无参数构造函数调用不同的构造函数:

public Rectangle() {
    this(1.0, 1.0, "RED", true);
}

您应该始终在构造函数中为对象属性设置一个值。在您的情况下,您可以定义默认构造函数来调用带有参数的构造函数,并将默认值传递给它:

public Rectangle() { 
    this(1.0, 1.0, "RED", true);
 }

您应该始终在构造函数中为对象属性设置一个值。在您的情况下,您可以定义默认构造函数来调用带有参数的构造函数,并将默认值传递给它:

public Rectangle() { 
    this(1.0, 1.0, "RED", true);
 }

我不完全理解这个问题,但这可能会有帮助,如果你想知道其他事情,就说吧

public Rectangle() {
    this(1.0,1.0,"RED",true);
}

public Rectangle(double width, double length) {
    this.width = width;
    this.length = length;
}

我不完全理解这个问题,但这可能会有帮助,如果你想知道其他事情,就说吧

public Rectangle() {
    this(1.0,1.0,"RED",true);
}

public Rectangle(double width, double length) {
    this.width = width;
    this.length = length;
}

您可以在构造函数中定义默认值

public class Rectangle extends Shape {

    private final double DEFAULT_WIDTH=1.0;
    private final double DEFAULT_LENGTH=1.0;
    private final String DEFAULT_COLOUR="RED";
    private final boolean DEFAULT_FILLED=true;

    protected double width;
    protected double length;

    public Rectangle() {
        this(DEFAULT_WIDTH, DEFAULT_LENGTH, DEFAULT_COLOUR, DEFAULT_FILLED); 
    }

    public Rectangle(double width, double length) {
        super();
        this.width = width;
        this.length = length;
    }

    public Rectangle(double width, double length, String color, boolean filled) {
        super(color, filled);
        this.width = width;
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }   

    public double getArea() {
        return (width * length);
    }

    public double getPerimeter() {
        return (2 * (width + length));
    }

    public String toString() {
        if (super.isFilled()) {
        return width + " x " + length + ", " + "filled with RED";
        } else {
        return width + " x " + length + ", " + "color: RED";
        }
    }
}

您可以在构造函数中定义默认值

public class Rectangle extends Shape {

    private final double DEFAULT_WIDTH=1.0;
    private final double DEFAULT_LENGTH=1.0;
    private final String DEFAULT_COLOUR="RED";
    private final boolean DEFAULT_FILLED=true;

    protected double width;
    protected double length;

    public Rectangle() {
        this(DEFAULT_WIDTH, DEFAULT_LENGTH, DEFAULT_COLOUR, DEFAULT_FILLED); 
    }

    public Rectangle(double width, double length) {
        super();
        this.width = width;
        this.length = length;
    }

    public Rectangle(double width, double length, String color, boolean filled) {
        super(color, filled);
        this.width = width;
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }   

    public double getArea() {
        return (width * length);
    }

    public double getPerimeter() {
        return (2 * (width + length));
    }

    public String toString() {
        if (super.isFilled()) {
        return width + " x " + length + ", " + "filled with RED";
        } else {
        return width + " x " + length + ", " + "color: RED";
        }
    }
}

if width==0{-比较,不是赋值。没有空参数。if width==0{-比较,不是赋值。没有空参数。更好:公共矩形{this1.0,1.0,红色,true;}更好:公共矩形{this1.0,1.0,红色,true;}