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

黄舟
Libérer: 2017-02-10 15:36:57
original
1356 Les gens l'ont consulté

    类,是面向对象语言的基础。类的三大特性:封装、继承、多态。最基本的特性就是封装性。
程序员用程序描述世界,将世界的所有事物都看成对象,怎么描述这个对象?那就是类了。也就是用类来封装对象。用书上的话说,类是具有相同属性和行为的对象的抽象。宝马汽车、别克汽车、五菱之光汽车... 基本具有相同的属性和行为,所以可以抽象一个汽车类,当然也可以把路人甲的宝马汽车、路人乙的别克汽车... 抽象一个汽车类。
类抽象完成后,可以实例化,实例化后的称之为一个对象,然后可以对属性赋值或运行类的方法。属性和方法同每个对象关联,不同的对象有相同的属性,但属性值可能不同;也具有相同的方法,但方法运行的结果可能不同。
类的属性和方法是被类封装的。
看如下类的定义:

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一样,但是不能被其它程序集访问,而且子类的话,只能被同一个命名空间的子类访问到。
    }
}
Copier après la connexion
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();
        }
    }
}
Copier après la connexion

结果:

以上就是C#基础知识整理:基础知识(2) 类 的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!