Home Backend Development C#.Net Tutorial C# basic knowledge compilation: basic knowledge (4) inheritance

C# basic knowledge compilation: basic knowledge (4) inheritance

Feb 10, 2017 pm 03:40 PM

As mentioned earlier, the three major characteristics of object-oriented: encapsulation, inheritance and polymorphism. We have almost fully understood the encapsulation in the definition of the previous class. Now let’s look at the characteristics of inheritance.
Inheritance is actually an extension of one class to another class. The latter is called a base class, and the former is called a subclass. Inheritance means that the subclass has all the attributes and methods of the base class, and the subclass can also add attributes and methods. However, subclasses cannot remove the attributes and methods of the parent class.
Of course, the issue of modifiers is also mentioned here. The subclass has all the properties and methods of the base class, but it does not mean that the subclass can arbitrarily access these inherited properties and methods. Subclasses can only access public and protected properties and methods, and the rest cannot be accessed directly. Another thing is that static properties and methods cannot be inherited, because the static type is related to the class and has nothing to do with the object.
Look at the code:

using System;

namespace YYS.CSharpStudy.MainConsole
{
    public class YSchool
    {
        private int id = 0;

        private string name = string.Empty;

        public int ID
        {
            get
            {
                return this.id;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }
       /// <summary>
       /// 构造器
       /// </summary>
        public YSchool()
        {
            this.id = 0;

            this.name = @"清华大学附中";
        }
        /// <summary>
        /// 构造器
        /// </summary>
        public  YSchool(int id, string name)
        {
            this.id = id;

            this.name = name;
        }
        /// <summary>
        /// 构造器
        /// </summary>
        public  YSchool(int id)
        {
            this.id = id;

            this.name = @"陕师大附中";
        }
    }

    public class YTeacher
    {
        private int id = 0;

        private string name = string.Empty;

        private YSchool school = null;

        private string introDuction = string.Empty;

        private string imagePath = string.Empty;

        /// <summary>
        /// 使用只读属性,因为是固有属性。
        /// </summary>
        public int ID
        {
            get
            {
                return id;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        /// <summary>
        /// 这几个使用get/set属性,因为这几个属性不是固有的,可以随着某些条件改变的。
        /// </summary>
        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;
            }
        }

        public YTeacher(int id, string name)
        {
            this.id = id;

            this.name = name;
        }

        /// <summary>
        /// 给学生讲课的方法
        /// </summary>
        public void ToTeachStudents()
        {
            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));
        }
    }
    /// <summary>
    /// 男性老师,继承自YTeacher
    /// </summary>
    public class MrTeacher : YTeacher
    {
        /// <summary>
        /// 构造器,这里要注意,YTeacher是没有提供默认构造器的,
        /// 所以子类必须要有构造器,并且和基类参数列表一致。
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public MrTeacher(int id, string name)

            : base(id, name)
        {

        }
        /// <summary>
        /// 扩展的方法,刮胡子方法。
        /// </summary>
        public void Shave()
        {
            Console.WriteLine(string.Format(@"{0} 老师用飞科剃须刀刮胡子。",this.Name));
        }
    }
    /// <summary>
    /// 女性老师,继承自YTeacher
    /// </summary>
    public class MisTeacher : YTeacher
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public MisTeacher(int id, string name)

            : base(id, name)
        {

        }
        /// <summary>
        /// 扩展方法,护肤的方法
        /// </summary>
        public void SkinCare()
        {
            Console.WriteLine(string.Format(@"{0} 老师用香奈儿护肤霜护肤。",this.Name));
        }
    }
}
Copy after login
using System;

namespace YYS.CSharpStudy.MainConsole
{
    
    class Program
    {
        static void Main(string[] args)
        {
            MrTeacher mrTeacher = new MrTeacher(1, @"牛轰轰");

            mrTeacher.ToTeachStudents();

            mrTeacher.PunishmentStudents(@"背唐诗");

            mrTeacher.Shave();

            MisTeacher misTeacher = new MisTeacher(2, @"郝漂靓");

            misTeacher.ToTeachStudents();

            misTeacher.PunishmentStudents(@"默写红楼梦");

            misTeacher.SkinCare();

            Console.ReadKey();
        }
    }
Copy after login

Result:

Inheritance is an object-oriented feature, and its benefits are:
First, when doing project design classes , inheritance allows us to omit a lot of code;
Second, it conforms to the organizational form of classes in object-oriented. In advanced object-oriented languages ​​such as C# and Java, there is an object class, which is the ancestor class of all classes. All classes are derived from him.
One thing you need to pay attention to when inheriting is the constructor of subclasses. During the program running process, the subclass first calls the constructor of the parent class. If the subclass does not write a constructor, the default constructor of the parent class will be called. If the parent class does not have a default constructor, that is, the parent class writes a constructor with parameters, then the subclass will also call the constructor with parameters, but you need to specify which constructor with parameters is called (see the base in the code ).
The above is the summary of C# basic knowledge: basic knowledge (4) inheritance content. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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.

Access Modifiers in C# Access Modifiers in C# Sep 03, 2024 pm 03:24 PM

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

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.

C# StringReader C# StringReader Sep 03, 2024 pm 03:23 PM

Guide to C# StringReader. Here we discuss a brief overview on C# StringReader and its working along with different Examples and Code.

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.

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.

BinaryWriter in C# BinaryWriter in C# Sep 03, 2024 pm 03:22 PM

Guide to BinaryWriter in C#. Here we discuss syntax and explanation, how it works with examples to implement with proper codes.

See all articles