C#---第十九课:不同类型方法的执行顺序(new / virtual / common / override)

本文介绍不同类型的方法,在代码中的执行顺序问题:

  • 构造方法
  • 普通方法(暂用common代替)、虚方法(Virtual修饰)、New方法(new修饰)三个优先级相同
  • overide方法(会替换virtual方法,此时virtual方法被隐藏,无法再调用到)

1. 构造函数方法(constructor)优先级最高,new/common/virtual/这三个修饰的方法优先级相同

  • 当父类、子类中的方法都是new/common/virtual/这三类的时候,可以理解为实例化对象的类型(等号左边的类型)是什么,就优先调用哪个类中的方法。
  • 父类初始化之后,无法生成子类的实例化对象。因整个过程,没有初始化子类的过程,不会生成子类对象。
csharp 复制代码
using ConsoleDeomAlien;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleDeomAlien
{

    public class Product
    {
        public Product()
        {
            Console.WriteLine("old Constructor-------Product");
        }

        public void Intro()
        {
            Console.WriteLine("old method--------Intro\n");
        }
    }

    public class NewProduct : Product
    {
        public NewProduct()
        {

            Console.WriteLine("new Constructor-------NewProduct");
        }
        
		// 这里使用new修饰或不用new都可以。都代表对父类进行重写。
		// 如果父类注定要被子类重写的,父类中可以写个空方法,后续子类直接重写即可。
        public new void Intro()  
        {
            Console.WriteLine("new method--------Intro\n");
        }
    }


    public class MainMethod
    {
        public static void Main()
        {
            Product p = new Product();		
            // 初始化和实例对象是相同的,此时该class中的方法仅仅是被重写(未被覆盖),改方法还起作用。
            // 最终,有限调用自己class中的方法
            p.Intro();

            NewProduct np = new NewProduct();
            // 子类优先调用该类下面的方法。
            np.Intro();

            Product p_np = new NewProduct();
            // 初始化对象是用的子类,但是对象是父类类型,最终会优先调用父类的方法。
            p_np.Intro();


			// !!!不符合逻辑的实例化过程,因为父类初始化之后,没有调用子类的构造函数,无法生成子类的实例对象!!!
			// NewProduct np_p = new Product ();



            Console.ReadKey();
        }
    }
}
csharp 复制代码
old Constructor-------Product
old method--------Intro

old Constructor-------Product
new Constructor-------NewProduct
new method--------Intro

old Constructor-------Product
new Constructor-------NewProduct
old method--------Intro

2. virtual / override 修饰的父、子类中的方法,最终virtual方法会被覆盖(且virtual被隐藏,无法起作用)

csharp 复制代码
using ConsoleDeomAlien;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleDeomAlien
{

    public class Product
    {
        public virtual void Intro()
        {
            Console.WriteLine("old method--------Intro\n");
        }
    }

    public class NewProduct : Product
    {

        public override void Intro()
        {
            Console.WriteLine("new method--------Intro\n");
        }
    }


    public class MainMethod
    {
        public static void Main()
        {
            Product p = new Product();
            p.Intro();

            NewProduct np = new NewProduct();
            np.Intro();

			// 虽然实例对象是父类,但是调用不到父类的方法了,因为被隐藏了。
            Product p_np = new NewProduct();  
            p_np.Intro();

            Console.ReadKey();
        }
    }
}
csharp 复制代码
old method--------Intro

new method--------Intro

new method--------Intro
  • 构造方法的优先级最高
  • 父类中的void方法会被子类中的普通(common)方法或virtual 方法覆盖。virtual & common 同等级。
  • 当子类中没override的时候,new会覆盖virtual / common
  • 当override存在时,virtual会被替代掉(virtual可以父类无法调用到virtual中的方法)
相关推荐
secondyoung1 天前
Markdown转换为Word:Pandoc模板使用指南
开发语言·经验分享·笔记·c#·编辑器·word·markdown
andyguo1 天前
AI模型测评平台工程化实战十二讲(第五讲:大模型测评分享功能:安全、高效的结果展示与协作)
人工智能·安全·c#
大飞pkz1 天前
【设计模式】访问者模式
开发语言·设计模式·c#·访问者模式
LateFrames2 天前
用 【C# + Winform + MediaPipe】 实现人脸468点识别
python·c#·.net·mediapipe
R-G-B2 天前
【14】C#实战篇——C++动态库dll 接口函数将char* strErr字符串 传给C# ,并且在winform的MessageBox和listbox中显示。C++ string 日志传给 C#
c++·c#·strerr字符串传给c#·动态库dll传递字符串给c#·string日志传给c#·c++ string传给 c#·c++底层函数日志传给c#显示
我是唐青枫2 天前
深入掌握 FluentMigrator:C#.NET 数据库迁移框架详解
数据库·c#·.net
tiankongdeyige2 天前
Unity学习之C#的反射机制
学习·unity·c#
绿荫阿广2 天前
用纯.NET开发并制作一个智能桌面机器人(六):使用.NET开发一个跨平台功能完善的小智AI客户端
c#·.net·asp.net core·maui·winui
周杰伦fans2 天前
c#设计模式—访问者模式
c#·.net
疯狂的Alex2 天前
【C#避坑实战系列文章15】C# WinForm 上位机开发:解决串口粘包+LiveCharts卡顿+InfluxDB存储(免费代码+仿真工具)
sqlite·c#·上位机·串口通信·livechars·c#硬件对接