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中的方法)
相关推荐
ghost14344 分钟前
C#学习第22天:网络编程
开发语言·学习·c#
神仙别闹1 小时前
基于C#实现中央定位服务器的 P2P 网络聊天系统
服务器·网络·c#
阿蒙Amon2 小时前
DevExpress&WinForms-TreeList-数据绑定
c#·devexpress·winforms
bicijinlian4 小时前
.Net HttpClient 使用代理功能
c#·.net·httpclient·.net httpclient·httpclient 代理
敲代码的 蜡笔小新8 小时前
【行为型之中介者模式】游戏开发实战——Unity复杂系统协调与通信架构的核心秘诀
unity·设计模式·c#·中介者模式
程序猿多布9 小时前
使用Visual Studio将C#程序发布为.exe文件
c#·visual studio
老衲有点帅10 小时前
C#多线程Thread
开发语言·c#
PascalMing12 小时前
C# 通过脚本实现接口
c#·codeanalysis·接口派生
敲代码的 蜡笔小新15 小时前
【行为型之观察者模式】游戏开发实战——Unity事件驱动架构的核心实现策略
观察者模式·unity·设计模式·c#
向宇it15 小时前
【unity游戏开发——编辑器扩展】使用EditorGUI的EditorGUILayout绘制工具类在自定义编辑器窗口绘制各种UI控件
开发语言·ui·unity·c#·编辑器·游戏引擎