我对封装有很大的困惑。我关于封装的概念是它仅用于数据隐藏。 我的代码:关于封装的概念
class Program
{
static void Main(string[] args)
{
Shape a;
a= new Shape();
a.Area = 4;
Console.WriteLine(a.Area);
}
}
class Shape
{
private int _area = 0;
private int _parameter = 0;
public int Area { get; set; }
public int Parameter { get; set; }
}
现在这里,私营int变量命名_area现在封装 按我的概念。所以,封装在这里做的是通过 使用私有访问修饰符,我可以隐藏这个变量和 通过使用公共属性,我可以初始化这个变量而不使用 使用类变量。
这是关于封装的一切吗?如果我错了,请纠正我。
请参阅:http://stackoverflow.com/questions/385361/what-are-the-different-types-of-encapsulation(除了顶部的答案)。 – Corak
作为一个方面说明:您的variabel'_area'永远不会被使用。当使用autoproperties('{get; set;}'语法)时,后台文件会在场景*后自动创建*。 – Koen
可能重复[为什么在没有逻辑涉及时使用属性?](http://stackoverflow.com/questions/11165326/why-use-properties-when-no-logic-is-involved) –