求助一个问题
大家好!我想问的是下题中?处,我用有参构造初始化了,
为什么方法结果和无参一样0 0 0,而不是8 16 15 呢。
//这是Main下的程序
//对象1
Rectangle1 rec = new Rectangle1();
Console.WriteLine(rec.and()); 程序执行后输出0
Console.WriteLine(rec.perimeter()); 程序执行后输出0
Console.WriteLine(rec.area()); 程序执行后输出0
Console.WriteLine(rec.and(3, 5)); 程序执行后输出8
Console.WriteLine(rec.perimeter(3, 5)); 程序执行后输出16
Console.WriteLine(rec.area(3, 5)); 程序执行后输出15
//对象2
Rectangle1 rec1 = new Rectangle1(3, 5);
Console.WriteLine(rec.and()); 程序执行后输出0 ?
Console.WriteLine(rec.perimeter()); 程序执行后输出0 ?
Console.WriteLine(rec.area()); 程序执行后输出0 ?
Console.WriteLine(rec.and(4, 6)); 程序执行后输出10
Console.WriteLine(rec.perimeter(4, 6)); 程序执行后输出20
Console.WriteLine(rec.area(4, 6)); 程序执行后输出24
//这是类中的程序
class Rectangle1
{
//两个成员变量
private int len;
private int wid;
//求和
public int and()
{
int an = len + wid;
return an;
}
public int and(int len,int wid)
{
int an = len + wid;
return an;
}
//求周长
public double perimeter()
{
double per = 2 * len + 2 * wid;
return per;
}
public double perimeter(int len,int wid)
{
double per = 2 * len + 2 * wid;
return per;
}
//求面积
public double area()
{
double are = len * wid;
return are;
}
public double area(int len, int wid)
{
double are = len * wid;
return are;
}
//构造函数无参合有参的
public Rectangle1()
{
len = 0;
wid = 0;
}
public Rectangle1(int len,int wid)
{
this.len = len;
this.wid = wid;
}
}