C# 的面向对象特性之封装

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using lesson8Another;//这里就可以使用,一个工程下的另一个,类
using AnotherAssemble;//在右边Solution explorer下 加入assemble 后再引入命名空间
/// <summary>
/// 1.C#的封装和public/private
///2.C#的封装和internal/protected
/// </summary>
namespace lesson8
{
    class Program
    {
        static void Main(string[] args)
        {
            //为的是让外部调用public
            //外部都不能调用(在class之外都不能访问)private
            //
            //public,private,internal,protected,internal protected
        }
        //internal 在一个程序集内可见,及在一个assemly的其他namespace均可见
        //protected 在本身class可见 ,以及继承他的class可见
        //internal protected 把上面的有点结合起来
        //assembly vs namespace
        //assembly 就是一个物理上包的概念
        //namespace逻辑上的概念 包括许多类包含在一起 一个项目可以有多个命名空间
        AnotherNamespaceClass ac = new AnotherNamespaceClass();//是另一个命名空间
        //意味着在一个程序集中间,命名空间是可以有多个的

    }

    class Person
    {
        private int age;//一般字段是用private

        //要想获取数据有两种方法
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        //方法是一般是public
        public int GetAge()
        {
            if (CheckAge())
            {
                return age;
            }
            return -1;
        }

        private bool CheckAge()
        {
            if (age <= 0)
            {
                return false;
            }
            return true;
        }

    }
}
在一个solution 下面添加一个assemble及new project

 在一个assemble下面添加一个class 及可以修改他的namespace

 

猜你喜欢

转载自xuyi1994.iteye.com/blog/2231002