Home > Backend Development > C#.Net Tutorial > C# basic knowledge compilation: basic knowledge (2) category

C# basic knowledge compilation: basic knowledge (2) category

黄舟
Release: 2017-02-10 15:36:57
Original
1443 people have browsed it

Classes are the basis of object-oriented languages. The three major characteristics of classes: encapsulation, inheritance, and polymorphism. The most basic feature is encapsulation.
Programmers use programs to describe the world and regard everything in the world as objects. How to describe this object? That's the class. That is, classes are used to encapsulate objects. In book terms, a class is an abstraction of objects with the same properties and behavior. BMW cars, Buick cars, Wuling Zhiguang cars... basically have the same attributes and behaviors, so you can abstract a car class. Of course, you can also abstract the BMW car of passerby A and the Buick car of passerby B... Abstract a car class .
After the class abstraction is completed, it can be instantiated. After instantiation, it is called an object, and then you can assign values ​​to properties or run methods of the class. Properties and methods are associated with each object. Different objects have the same properties, but the property values ​​may be different; they also have the same methods, but the results of the method execution may be different.
The attributes and methods of a class are encapsulated by the class.
Look at the definition of the following class:

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

Result:

The above is the basic knowledge of C#: the content of basic knowledge (2) class, more For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template