封装:隐藏信息,并且留出对外的访问接口
访问修饰符:public公共的 private私有的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LeiheDuixiang
{
/// <summary>
/// 小朋友
/// </summary>
class Child
{
//隐藏信息,private不能在类外进行访问
private string name; //字段
private string sex;
private int age;
private int height;
/// <summary>
/// 完成字段的对外访问接口
/// 属性,属性的名字首字母应该大写
/// </summary>
public string Name
{
get { return name; }//读访问器,返回字段的值
set { name = value; } //写访问器,给字段赋值
}
public string Sex
{
get { return sex; }
set { sex = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public int Height
{
get { return height; }
set { height = value; }
}
public void PlayBall()
{
Console.WriteLine("Hello,我是帅哥");
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LeiheDuixiang
{
class Program
{
static void Main(string[] args)
{
Child xiaoming = new Child();//实例化了Child类的对对象
xiaoming.Name = "马晓明";
xiaoming.Sex = "男";
xiaoming.Age = 6;
xiaoming.Height = 120;
Console.WriteLine("我叫" + xiaoming.Name + "今年" + xiaoming.Age + "岁。");
xiaoming.PlayBall();
}
}
}