Pessoal, quando estou definindo um construtor, eu posso colocar o construtor com argumentos padrão?
Por exemplo
public Rectangle(double l = 0, double w = 0)
{
setValues(l,w);
}
Um construtor desse tipo não funciona pra mim, eu tive que fazer uma sobrecarga de construtores , eis a classe completa:
[code]public class Rectangle {
private double length;
private double width;
public Rectangle() // constructor without argument
{
this(0,0); // chama o construtor sem paramentros
}
public Rectangle(double l, double w)
{
setValues(l,w);
}
public void setValues(double le,double wi)
{
setLength(le);
setWidth(wi);
}
///////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
public double perimeter()
{
return getLength()*2 + getWidth()*2;
}
public double area()
{
return getLength() * getWidth();
}
public void setLength(double len)
{
length =(len<=0)?1:len;
}
public void setWidth(double wid)
{
width=(wid<=0)? 1:wid;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
}
[/code]
Eu fiz algo errado ou o java nao aceita um construtor com argumentos default e eu sempre tenho que fazer a sobrecarga?
[]'s