C#在第一个类里面设置了set先验证是否符合条件再赋值,但是后来检验时发现没用啊,求指教!!
初学请多指教!!!usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Tex...
初学请多指教!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Rectangle
{
private float length = 1;
private float width = 1;
public float Width
{
get
{
return width;
}
set
{
if (value < 20 && value > 0)
width = value;
else width = 1;
}
}
public float Length
{
get
{
return length;
}
set
{
if (value < 20 && value > 0)
length = value;
else length = 1;
}
}
public float Area(float length, float width)
{
Console.WriteLine( );
return length * width;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" ");
float length = Convert.ToInt16(Console.ReadLine());
float width = Convert.ToInt16(Console.ReadLine());
Rectangle area = new Rectangle();
Console.WriteLine(area.Area(length, width));
Console.ReadLine();
}
}
} 展开
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Rectangle
{
private float length = 1;
private float width = 1;
public float Width
{
get
{
return width;
}
set
{
if (value < 20 && value > 0)
width = value;
else width = 1;
}
}
public float Length
{
get
{
return length;
}
set
{
if (value < 20 && value > 0)
length = value;
else length = 1;
}
}
public float Area(float length, float width)
{
Console.WriteLine( );
return length * width;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" ");
float length = Convert.ToInt16(Console.ReadLine());
float width = Convert.ToInt16(Console.ReadLine());
Rectangle area = new Rectangle();
Console.WriteLine(area.Area(length, width));
Console.ReadLine();
}
}
} 展开
1个回答
展开全部
你的代码中,并没有给属性赋值的代码,所以你写在属性中的验证代码没有起作用。
Console.WriteLine(" ");
Rectangle area = new Rectangle();
//给属性赋值
area.Length = Convert.ToInt16(Console.ReadLine());
area.Width = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(area.Area(area.Length, area.Width));
Console.ReadLine();
追问
我也感觉是这个原因,但是不知道怎么写才是对的...
追答
K7
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询