c# 不可访问 因为它受保护级别限制

来源:互联网 发布:咫尺网络小程序收费 编辑:程序博客网 时间:2024/06/14 18:08
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


 namespace PropertyText
{
    class Program
    {
        static void Main(string[] args)
        {
            classText ct = new classText("李刚");
            Console.WriteLine(ct.name);
            ct.Name = "刘明";
            Console.WriteLine(ct.Name);
            Console.ReadLine();
        }
    }
    class classText
    {
        private String name = String.Empty;
        public classText()
        {
            Console.WriteLine("测试类");


        }
        public classText(String name)
{
            this.name=name;

}
        public  String  Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
    }

}


显示 

错误 1 “PropertyText.classText.name”不可访问,因为它受保护级别限制

最后却发现因为这一行:

   Console.WriteLine(ct.name);   中 name 中的 N 应该大些之后就没有错误了。

现在还不知道为什么。。。但是这是c#第一篇博客了。。好的开始是成功的一半!加油!

求指点 为什么改了就没错误,没改就有错误啊!!!


自学了几天c#终于知道了原因。ct.Name是指ct里面的属性。而ct.name则是ct里的一个一个变量。

因为name在classText类中是private型,因此只能在classText类中使用。使用private是封装性的体现。


原创粉丝点击