您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

C#基础知识整理:基础知识(2) 类

2024/2/25 9:53:57发布13次查看
类,是面向对象语言的基础。类的三大特性:封装、继承、多态。最基本的特性就是封装性。
程序员用程序描述世界,将世界的所有事物都看成对象,怎么描述这个对象?那就是类了。也就是用类来封装对象。用书上的话说,类是具有相同属性和行为的对象的抽象。宝马汽车、别克汽车、五菱之光汽车... 基本具有相同的属性和行为,所以可以抽象一个汽车类,当然也可以把路人甲的宝马汽车、路人乙的别克汽车... 抽象一个汽车类。
类抽象完成后,可以实例化,实例化后的称之为一个对象,然后可以对属性赋值或运行类的方法。属性和方法同每个对象关联,不同的对象有相同的属性,但属性值可能不同;也具有相同的方法,但方法运行的结果可能不同。
类的属性和方法是被类封装的。
看如下类的定义:
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) 类 的内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product