顺序员用顺序形貌天下,将天下的一切事物都算作对象,怎样形貌这个对象?那就是类了。也就是用类来封装对象。用书上的话说,类是具有雷同属性和行动的对象的笼统。宝马汽车、别克汽车、五菱之光汽车... 基础具有雷同的属性和行动,所以能够笼统一个汽车类,固然也能够把路人甲的宝马汽车、路人乙的别克汽车... 笼统一个汽车类。
类笼统完成后,能够实例化,实例化后的称之为一个对象,然后能够对属性赋值或运转类的要领。属性和要领同每一个对象关联,差别的对象有雷同的属性,但属性值能够差别;也具有雷同的要领,但要领运转的效果能够差别。
类的属性和要领是被类封装的。
看以下类的定义:
using System; namespace YYS.CSharpStudy.MainConsole { /// <summary> /// 定义一个学校类 /// 这个类只要属性,没有要领(实在确实的来说是有一个默许的组织器要领) /// </summary> public class YSchool { /// <summary> ///字段, 类内里定义的变量称之为“字段” /// 保留学校的ID /// </summary> private int id = 0; /// <summary> /// 保留学校的名字 /// </summary> private string name = string.Empty; /// <summary> /// 属性,字段作为保留属性值的变量,而属性则有特别的“行动”。 /// 运用get/set来示意属性的行动。get取属性值,set给属性赋值。因而get/set称为“接见器”。 /// /// ID属性 /// </summary> public int ID { get { //get返回一个值,示意当前对象的该属性的属性值。 return this.id; } //这里的.号用于接见对象的属性或要领。 //this指当前对象,意即哪一个实例在操纵属性和要领,this就指哪一个实例。 set { //局部变量value,value值是用于外部赋给该该属性的值。 this.id = value; } } /// <summary> /// 姓名属性 /// </summary> public string Name { get { return name; } set { name = value; } } } public class YTeacher { private int id = 0; private string name = string.Empty; //这里将YSchool类作为了YTeacher的一个属性。 private YSchool school = null; private string introDuction = string.Empty; private string imagePath = string.Empty; public int ID { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } public YSchool School { get { if (school == null) { school = new YSchool(); } return school; } set { school = value; } } public string IntroDuction { get { return introDuction; } set { introDuction = value; } } public string ImagePath { get { return imagePath; } set { imagePath = value; } } /// <summary> /// 给门生授课的要领 /// </summary> public void ToTeachStudents() { //{0},{1},{2}是占位符,对应背面的参数。平常假如显现的内容中含有参数,我比较喜好用string.Format。 Console.WriteLine(string.Format(@"{0} 先生教诲同学们: Good Good Study,Day Day Up!", this.name)); } /// <summary> /// 责罚犯错误门生的要领 /// </summary> /// <param name="punishmentContent"></param> public void PunishmentStudents(string punishmentContent) { Console.WriteLine(string.Format(@"{0} 的{1} 先生让犯错误的门生 {2}", this.school.Name, this.name, punishmentContent)); } //字段、属性和要领前润饰符有:public,private,protected,internal //public,字段、属性和要领均为公然的,不仅类中的别的成员能接见到,还能够经由过程类的实例接见的到。 //private,字段、属性和要领均为私有的,只能被类中的别的成员接见到,不能经由过程类的实例接见。 //protected,包括private特征,而且protected润饰的字段、属性和要领能被子类接见到。 //internal,在同一个顺序集中和public一样,然则不能被别的顺序集接见,而且子类的话,只能被同一个定名空间的子类接见到。 } }
using System; namespace YYS.CSharpStudy.MainConsole { class Program { static void Main(string[] args) { //实例化详细对象,而且赋值 YSchool shool1 = new YSchool(); shool1.ID = 1; shool1.Name = "清华附中"; YSchool school2 = new YSchool(); school2.ID = 2; school2.Name = "北师大附中"; YTeacher techerS = new YTeacher(); techerS.ID = 1; techerS.Name = @"尚进"; techerS.School = shool1; techerS.IntroDuction = @"很严肃"; techerS.ImagePath = @"http://"; //运转当前实例的要领 techerS.ToTeachStudents(); //运转当前实例的要领,传入参数 techerS.PunishmentStudents(@"抄一切学过的唐诗一百遍"); Console.WriteLine(); YTeacher techerQ = new YTeacher(); techerQ.ID = 2; techerQ.Name = @"秦奋"; techerQ.School = school2; techerQ.IntroDuction = @"和颜悦色"; techerQ.ImagePath = @"http://"; techerQ.ToTeachStudents(); techerQ.PunishmentStudents(@"抄一切学过的数学公式一遍"); Console.ReadKey(); } } }
效果:
以上就是C#基础知识整顿:基础知识(2) 类 的内容,更多相关内容请关注ki4网(www.ki4.cn)!