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

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

Feb 10, 2017 pm 03:36 PM

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 (www.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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

See all articles