当前位置: 首页 > news >正文

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

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

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


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

  • 当父类、子类中的方法都是new/common/virtual/这三类的时候,可以理解为实例化对象的类型(等号左边的类型)是什么,就优先调用哪个类中的方法。
  • 父类初始化之后,无法生成子类的实例化对象。因整个过程,没有初始化子类的过程,不会生成子类对象。

在这里插入图片描述



在这里插入图片描述


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();}}
}

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


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

在这里插入图片描述

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();}}
}

old method--------Intronew method--------Intronew method--------Intro
  • 构造方法的优先级最高
  • 父类中的void方法会被子类中的普通(common)方法或virtual 方法覆盖。virtual & common 同等级。
  • 当子类中没override的时候,new会覆盖virtual / common
  • 当override存在时,virtual会被替代掉(virtual可以父类无法调用到virtual中的方法)
http://www.lryc.cn/news/147975.html

相关文章:

  • lnmp架构-PHP
  • 【javascript实操记录】
  • Mysql--技术文档--悲观锁、乐观锁-《控制并发机制简单认知、深度理解》
  • 【GO】LGTM_Grafana_Tempo(2)_官方用例改后实操
  • git 口令
  • 【回眸】剑指offer(二)解题思路
  • Python 基本文件操作及os库
  • YOLOv5算法改进(9)— 替换主干网络之ShuffleNetV2
  • 三、mycat分库分表
  • gitlab提交项目Log in with Access Token错误
  • openGauss学习笔记-56 openGauss 高级特性-DCF
  • Xcode 14 pod init报错
  • 飞腾PSPA可信启动--2 数字签名证书
  • 微前端:重塑大型项目的前沿技术
  • 官方推荐使用的OkHttp4网络请求库全面解析(Android篇)
  • Spooling的原理
  • Homebrew 无法安装过时的PHP版本
  • python爬取bilibili,下载视频
  • java八股文面试[多线程]——进程与线程的区别
  • SpringBootWeb 登录认证[Cookie + Session + Token + Filter + Interceptor]
  • d3dcompiler_43.dll丢失怎么修复,分享几种修复d3dcompiler_43.dll的方法
  • mqtt集群搭建并使用nginx做负载均衡_亲测得结论
  • JavaScript—DOM(文档对象模型)
  • mysql Index
  • ​八路参考文献:[八一新书]许少辉.乡村振兴战略下传统村落文化旅游设计[M]北京:中国建筑出版传媒,2022.
  • Leetcode Top 100 Liked Questions(序号75~104)
  • Shell编程之流程控制
  • 什么是Python爬虫分布式架构,可能遇到哪些问题,如何解决
  • QT下使用ffmpeg+SDL实现音视频播放器,支持录像截图功能,提供源码分享与下载
  • Microsoft Excel整合Python:数据分析的新纪元