10-封装

基本的封装同C++类似

using System;

namespace RectangleApplication
{
    class Rectangle
    {
        //成员变量
        internal double length;
        internal double width;
        
        double GetArea() // 默认私有
        {
            return length * width;
        }
       public void Display()
        {
            Console.WriteLine("长度: {0}", length);
            Console.WriteLine("宽度: {0}", width);
            Console.WriteLine("面积: {0}", GetArea());
        }
        private void Display()
        {}
        protected void Display()
        {}
    }//end class Rectangle    
    class ExecuteRectangle
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            r.length = 4.5;
            r.width = 3.5;
            r.Display();
            Console.ReadLine();
        }
    }
}    

  

internal 关键字:

作用域仅在本DLL中。

protected internal:

作用域仅在本DLL和被继承的DLL中。

参考:

http://www.runoob.com/csharp/csharp-encapsulation.html

https://blog.csdn.net/baidu_32134295/article/details/51285603

猜你喜欢

转载自www.cnblogs.com/alexYuin/p/9067578.html
今日推荐