using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 类1 { class Program { static void Main(string[] args) { People p = new People(); p.name = "tom"; p.age = 20; p.hight = 180; } } //演示public和private区别 class People { public string name; public int age; private int hight; } }
上面这段代码,编译时会提示:
但是,private类型的hight可以在people类的内部使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 类1 { class Program { static void Main(string[] args) { People p = new People(); p.name = "tom"; p.age = 20;
//此处仍然会出错 p.hight = 180; } } //演示public和private区别 class People { public string name; public int age; private int hight; public void speak() { Console.WriteLine("我的名字叫:{0},我的年龄是:{1},我的身高是:{2}",this.name,this.age,this.hight);