如何处理Java中的空参数?

时间:2015-08-10 06:21:05

标签: 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, 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){}作为宽度,但因为它是原始类型所以它不起作用...任何人都可以告诉我如果空参数是什么我可以打印出默认值用过的 ?将不胜感激...谢谢!

4 个答案:

答案 0 :(得分:12)

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

public Rectangle() {
    super("RED", true);
    this.width = 1.0;
    this.length = 1.0;
}

或者您可以在成员声明中指定默认值:

protected double width = 1.0;
protected double length = 1.0;

并且具有类似的声明,其中包含Shape成员的默认值。

或者你可以从无参数构造函数中调用另一个构造函数,如Seelenvirtuose所建议的那样:

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

我不完全理解这个问题,但是如果你想要别的东西,那么这可能会有所帮助

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

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

答案 3 :(得分:0)

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

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";
        }
    }
}