封装是数据绑定的过程。 这意味着封装来自外部访问的数据。 在面向对象的编程中,封装起着非常重要的作用,因为封装提供了保护数据免受意外损坏的作用。
C#封装示例
class Rectangle
{
public double length;
public double width;
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 6.5;
r.width = 5.5;
r.Display();
Console.ReadLine();
}
}
日期:2020-04-11 22:50:28 来源:oir作者:oir
