一,人类的起源,及人类的爬,走,跑是怎么炼成的.
/// <summary>
/// 一个人
/// </summary>
public class People
{
/// <summary>
/// 默认构造函数,建议要有一个
/// </summary>
public People()
{
}
/// <summary>
/// 只初始化人的姓名
/// </summary>
/// <param name="name"></param>
public People(string name)
{
this.Name = name;
}
/// <summary>
/// 只初始化人的姓名和性别
/// </summary>
/// <param name="name"></param>
/// <param name="gander"></param>
public People(string name, bool gander)
: this(name)
{
this.Gander = gander;
}
/// <summary>
/// 只和类本身有关的属性,我们把它做成静态的
/// </summary>
public static string ClassName = "人类";
/// <summary>
/// 在程序运行期间不变的做为常量
/// </summary>
public const double PI = 3.1415926;
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 性别,1为女,0为男
/// </summary>
public bool Gander { get; set; }
/// <summary>
/// 身份证
/// </summary>
public string NumID { get; set; }
/// <summary>
/// 地址
/// </summary>
public string Address { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int Age { get; set; }
}
如果我们希望建立一个叫小明的人,怎么办?
People 小明 = new People("小明");
Console.WriteLine(小明.Name + 小明.Gander); //注意bool型默认为False
如果建立一个叫小红的女人,就可以这样了
People 小红 = new People("小红", true);
Console.WriteLine(小红.Name +小红.Gander);
OK,这时,我们已经建立了两个人了,我想知道它们的身高体重,怎么办?我们可以想一个人的身高和体重是每个人固有的信息,我们可以把它们放在一个结构里面,就像这样,
/// <summary>
/// 描述人的身高和体重信息的结构体
/// </summary>
public struct HighWeight
{
/// <summary>
/// 只设置身高
/// </summary>
/// <param name="height"></param>
public HighWeight(int high)
: this()
{
this.High = high;
}
/// <summary>
/// 只设置体重
/// </summary>
/// <param name="weight"></param>
public HighWeight(double weight)
: this()
{
this.Weight = weight;
}
/// <summary>
/// 对身高和体重进行初始化
/// </summary>
/// <param name="height"></param>
/// <param name="weight"></param>
public HighWeight(int height, double weight)
: this(height)
{
this.Weight = weight;
}
/// <summary>
/// 身高
/// </summary>
public int High { get; private set; } //private属性只允许类内部对本属性赋值
/// <summary>
/// 体重
/// </summary>
public double Weight { get; private set; }
}
那我们在调用时,就是这样:
HighWeight heghweight = new HighWeight(175);
Console.WriteLine("身高:{0},体重:{1}", heghweight.High, heghweight.Weight);
HighWeight heghweight2 = new HighWeight(74.4);
Console.WriteLine("身高:{0},体重:{1}", heghweight2.High, heghweight2.Weight);
HighWeight heghweight3 = new HighWeight(175,74.4);
Console.WriteLine("身高:{0},体重:{1}", heghweight3.High, heghweight3.Weight);
事实上,身高体重是属于每个人的一种特性,所以,我们应该把它放在人的类上,于是代码就变成了这样:
/// <summary>
/// 一个人
/// </summary>
public class People
{
/// <summary>
/// 默认构造函数,建议要有一个
/// </summary>
public People()
{
}
/// <summary>
/// 只初始化人的姓名
/// </summary>
/// <param name="name"></param>
public People(string name)
{
this.Name = name;
}
/// <summary>
/// 只初始化人的姓名和性别
/// </summary>
/// <param name="name"></param>
/// <param name="gander"></param>
public People(string name, bool gander)
: this(name)
{
this.Gander = gander;
}
/// <summary>
/// 只和类本身有关的属性,我们把它做成静态的
/// </summary>
public static string ClassName = "人类";
/// <summary>
/// 在程序运行期间不变的做为常量
/// </summary>
public const double PI = 3.1415926;
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 性别
/// </summary>
public bool Gander { get; set; }
/// <summary>
/// 身份证
/// </summary>
public string NumID { get; set; }
/// <summary>
/// 地址
/// </summary>
public string Address { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int Age { get; set; }
/// <summary>
/// 身高和体重
/// </summary>
public HighWeight HighWeight { get; set; }
}
继承,这个概念不难理解,它是指一个具有公共对象的类被其它具有相似功能的类继承,其中被继承的类称为基类,所对应的类叫做它的子类,继承在大项目中用的不多,
因为继承会增加程序的耦合度(即类之间的相互依赖的程度).回到实例中来,所有人都会运动,无论是爬,走,还是跑,只是根据年龄不同,他们的运动所不同,看代码:
#region 方法
public virtual string Run()
{
return "刚出生的人只会爬";
}
#endregion
上面这段代码是People类的,下面是它的子类,将这个方法进行复写
/// <summary>
/// 满1岁的人
/// </summary>
public class Yang : People
{
/// <summary>
/// 调用它的父类的构造方法为父类属性赋值
/// </summary>
/// <param name="name"></param>
public Yang(string name)
: base(name)
{
}
public override string Run()
{
return "满一岁,人会走了";
}
}
下面是对类进行调用的过程:
People p = new People();
Console.WriteLine(p.Run());
Yang yang = new Yang("小孩");
Console.WriteLine("姓名:{0},它的运动:{1}", yang.Name, yang.Run());
接下来,我要说扩展方法了,顾名思义,就是定义在一个类外部的方法,但这个方法又可以作用在这个类上,看代码:
/// <summary>
/// 静态类,包括所以类的扩展方法
/// </summary>
public static class AllEvent
{
/// <summary>
/// People的扩展方法,方法和方法所在的类必须是static
/// </summary>
/// <param name="people"></param>
/// <param name="name"></param>
/// <returns></returns>
public static People GetPeopleByName(this List<People> people, string name)
{
return people.Where(i => i.Name.Equals(name)).SingleOrDefault();
}
}
调用的时候,可以直接使用它
List<People> peopleList = new List<People>()
{
new People("张三",true),
new People("李四",false),
};
Console.WriteLine(peopleList.GetPeopleByName("李四").Name);
接下来,就是一个概念性的东西, System.Nullable<T>,即可空的类型,它可以叫值类型也赋null这种值,定义如下:
Int? Pi //定义一个页签变量
Int pi=Pi ?? 1 //如果pi为空,然后将1赋值pi
1 类和结构,继承和方法复写及扩展方法及部分类
OK,我们来看一个问题,我们的人类people目前的结果如图
看起来,有点乱了,我们想把它的方法和属性分开,这样做,有两个好像,第一代码清晰,第二也是引用部分类partial的作用,就是有利于团队开发.(check in and check out )如图:
到些,第一讲就完了!好累呀!
作者:仓储大叔,张占岭,
荣誉:微软MVP