c# 中的继承操作

C# 继承的详细介绍和使用 阅读详情

作者:chen_h
微信号 & QQ:862251340
微信公众号:coderpai


继承是面向对象程序设计中最重要的概念之一。

在 C# 语言中仅支持单重继承,主要用于解决代码的重用问题。

继承允许我们根据一个类来定义另一个类,这使得创建和维护应用程序变得更容易。

同时也有利于重用代码和节省开发时间。

当创建一个类时,我们不需要完全重新编写新的数据成员和成员函数,只需要设计一个新的类,继承了已有的类的成员即可。

这个已有的类被称为的基类,这个新的类被称为派生类。

继承的思想实现了属于(IS-A)关系。例如,哺乳动物属于(IS-A)动物,狗属于(IS-A)哺乳动物,因此狗属于(IS-A)动物。

C# 继承的特点

  • 派生类是对基类的扩展,派生类可以添加新的成员,但不能移除已经继承的成员的定义。
  • 继承是可以传递的。如果 C 从 B 中派生,B 又从 A 中派生,那么 C 不仅继承了 B 中声明的成员,同样也继承了 A 中声明的成员。
  • 构造函数和析构函数不能被继承,除此之外其他成员能被继承。基类中成员的访问方式只能决定派生类能否访问它们。
  • 派生类如果定义了与继承而来的成员同名的新成员,那么就可以覆盖已继承的成员,但这并不是删除了这些成员,只是不能再访问这些成员。
  • 类可以定义虚方法、虚属性及虚索引指示器,它的派生类能够重载这些成员,从而使类可以展示出多态性。
  • 派生类只能从一个类中继承,可以通过接口来实现多重继承。

为了将继承关系灵活运地用到程序设计中,在 C# 语言中提供了接口来解决多重继承的关系。

在编程中灵活地使用类之间的继承关系能很好地实现类中成员的重用,有利于类的使用。

Object 类是 C# 语言中最原始、最重要的类,是所有类的“祖先”,每个 C# 类都是它的子类,它实现了每个类都必须具有的基本方法。

这里指的“所有类”,即不管是 C# 系统所提供的标准类,还是用户自行編写的类,都是从Object类直接或间接继承而来,它是类层次结构中的顶级类,即 C# 树型类层次结构的“根”。

Object 类中的属性和方法可以被用到任何类。

当編程者定义一个类时没有使用关键字 Extends 指明它的父类,则編译器认为该类从 Object 类继承而来。

但 Object 类的方法所实现的功能很有限,都是由编程者在自行设计的 class 类型的类体内编写与原型完全相同的方法来覆盖它们,以实现用户所要求的有用功能。

在 Object 类中提供了 4 个常用的方法,即 Equals、GetHashCode、GetType 以及 ToString 方法。

我们将在接下来的几节中分别为大家讲解这四种常用方法。

既然任何一个类都继承了 Object 类,这 4 个方法也可以被任何类使用或重写。

参考链接:http://m.biancheng.net/csharp/60/

C#中的继承(详解) 继承详解:一.继承的概念:二. 基类和派生类:三.继承的作用:四.继承的特点:五.举例说明:六.继承注意事项: 一.继承的概念: 继承:是面向对象程序设计中最重要的概念之一。继承允许我们根据一个类来定义另一个类,这使得创建和维护应用程序变得更容易。同时也有利于重用代码和节省开发时间。当创建一个类时,不需要完全重新编写新的数据成员和成员函数,只需要设计一个新的类,继承了已有的类的成员即可。这个已有的类被称为的基类/父类,这个新的类被称为派生类/子类。 二. 基类和派生类: 基类/父类:也可以理解成生活中的父亲 阅读详情

相关推荐

解密C# base关键字:深度探索继承体系中的核心操作与实战示例

在C#编程语言中,base关键字扮演着至关重要的角色,特别是在涉及继承关系的类结构中。它允许子类访问、调用或初始化基类的成员,从而实现代码复用、多态性以及继承体系的正确构建。本篇文章将深入剖析base关键字的各种用法,辅以详细代码示例和注释,帮助您全面掌握这一关键概念。

java专栏 806

C#对象的继承详解

C#中对象的继承,父类和子类的继承关系,以及抽象类与抽象方法的详解

Jackyoung66的博客 5357

Linux Component概述和高通component的使用

为了让subsystem按照一定顺序初始化设备才提出来的。subsystem中由很多设备模块,内核加载这些模块的时间不确定。子系统内有些模块是需要依赖其它模块先初始化才能进行自己初始化工作(例如v4l2 subdev和v4l2 video device),这时就要用到component框架。例如v4l2 subdev和v4l2 video device中,谁依赖谁先创建?

lz_1990的专栏 1611

C# 继承(inheritance)

派生类是对基类的扩展,派生类可以添加新的成员,但不能移除已经继承的成员的定义。继承是可以传递的。如果 C 从 B 中派生,B 又从 A 中派生,那么 C 不仅继承了 B 中声明的成员,同样也继承了 A 中声明的成员。构造函数和析构函数不能被继承,除此之外其他成员能被继承。基类中成员的访问方式只能决定派生类能否访问它们...

weixin_47336109的博客 3411

基于Simulink的IEEE33节点配电网仿真实战:从建模到分布式能源集成

本文详细介绍了基于Simulink的IEEE33节点配电网仿真实战,从基础建模到分布式能源集成。通过MATLAB脚本管理参数、搭建Simulink模块,并集成风机和光伏系统,实现高效配电网仿真。文章还涵盖潮流计算、故障分析及性能优化技巧,为电力系统仿真提供实用指导。

git9versioner的博客 993

Seedance2.5本地部署指南:从环境配置到生产优化的完整实践

在 AI 内容生成领域,本地部署方案正逐渐成为开发者和技术团队关注的焦点。Seedance2.5 作为一款支持图像和视频生成的 AI 工具,其完全离线运行的能力解决了云端服务在数据隐私、使用成本和访问稳定性方面的痛点。对于需要处理敏感内容、频繁生成素材或希望完全掌控生成流程的技术人员来说,掌握本地部署方法具有实际价值。 本文将以 Seedance2.5 的本地部署为例,详细介绍从环境准备、依赖配置到运行验证的完整流程,同时深入分析关键参数配置、常见问题排查方法以及生产环境下的优化建议。通过实际部署过程,理解

weixin_33139275的博客 737

C#基础知识点总结(三)- 继承

继承:虚方法,多态,隐藏,抽象,密封,派生类构造函数 修饰符:public等 接口 is和as

weixin_43152495的博客 3519

LinuxCNC 源码解析:从界面开发到硬件交互的实战指南

本文深入解析LinuxCNC源码架构,从用户界面开发到硬件交互的实战指南。涵盖GTK与Tkinter界面开发、HAL硬件抽象层、G代码解析器改造及运动控制核心等关键模块,提供定制化开发技巧与性能优化方案,助力开发者高效掌握数控系统开发。

weixin_27066963的博客 190

c#网络操作类继承关系图

自己总结的,给大家参考一下,

Hello World 974

别再手动拷贝文件了!用Keil MDK的Pack Installer一键搞定STM32上的RT-Thread Nano移植

本文详细介绍了如何使用Keil MDK的Pack Installer一键快速移植RT-Thread Nano到STM32项目,告别繁琐的手动拷贝和配置过程。通过自动化版本管理和依赖处理,移植时间从47分钟缩短至3分钟,大幅提升开发效率。文章还提供了实战步骤、高级配置技巧和常见问题解决方案,帮助开发者轻松实现RTOS集成。

weixin_29194817的博客 365

单片机实验五 静态数码管实验

1.熟悉实验软件和硬件,进行正确的接线;2.数码管依次显示数字0-F;

m0_51738372的博客 4416

c#文件系统操作类继承关系图

自己总结的,给大家参考一下,

Hello World 1447

2026年职业院校技能大赛中职移动应用与开发模块二10套题库开发培训视频(全套)

2026年职业院校技能大赛中职移动应用与开发模块二10套题库开发培训视频(全套)

我是网络安全兼技能大赛工程师,专注网络安全技术学习与技能大赛实战!持续分享最新的技术文章,帮你少走弯路,一起在技术赛道上进步~ 355

【C#】字符串的操作、堆和栈的区别、值类型和引用类型的区别、继承、Protected

一、字符串的操作 1.字符串转换成小写、大写的方法 ToLower():得到字符串的小写形式 ToUpper():得到字符串的大写形式 Trim():去掉字符串两端的空白 2.忽略大小写 s1.Equals(s2,StringComparison.OrdinallgnoreCase):两个字符串进行比较,不区分大小写的比较 3.字符串的分割 string Split(params ch...

华淑敏的博客 614

2023华为od机试C卷【启动多任务排序】C++ 实现

一个应用启动时,会有多个初始化任务需要执行,并且任务之间有依赖关系,例如A任务依赖B任务,那么必须在B任务执行完成之后,才能开始执行A任务。现在给出多条任务依赖关系的规则,请输入任务的顺序执行序列,规则采用贪婪策略,即一个任务如果没有依赖的任务,则立刻开始执行,如果同时有多个任务要执行,则根据任务名称字母顺序排序。

MISAYAONE的博客 889

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

C# 高级 子类继承父类 后 override 和 new的操作

父类中的虚方法 被子类继承后; 前提条件:父类 new 子类(); 子类重写后(override)会执行子类重写 的方法 子类隐藏后(new)会执行父类的方法 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { ...

欧巴酱的优快云 1111

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C#基础03 继承,多态,XML操作与IO流

01:什么是继承?代码举例说明 继承要符合 is a 关系,卡车继承汽车,卡车 is a同一 汽车。 子类通过:继承父类 /// <summary> /// 汽车类 /// </summary> class Car { } /// <summary> /// 卡车类 /// </summa...

qq_43227356的博客 197

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

C#拾遗系列(9):继承、接口、扩展方法、分部类、类操作、Ref and Out、可空类型...

本文内容: 继承 Equal示例 结构和类 属性 Ref and Out 类操作 扩展方法 接口 可空类型 分部类 1. 继承 using System; using System.Collections.Generic; using System.Linq; using...

weixin_30547797的博客 71

C# 基础知识扩充3 继承,多态,XML,文件流 操作

一:什么是继承?代码举例说明 子类传承父类的公有属性和方法或者构造函数 就是继承,可以通过 子类:父类 这种形式达到继承 关键字 : 最为重要 public class A { //被public修饰的属性方法,构造函数 } B类继承A类通过关键字 ( : ) public class B:A { //继承可以拿到父类被public修饰的属性...

qq_40514546的博客 210

C#中多重继承

其实想写这篇文章,是因为突然在整理论坛上的帖子的时候,突然发现一个人问我如何才能在C#中实现多重继承,当时我答的很隐晦,因此这里我想补充说明一下.首先,我要说明一下,C#中是没有类的多重继承这个概念.要使用多重继承必须要通过接口Interface来完成.可是大家都知道Interface实际上就是一个虚函数列表指针.内部封装的只有函数和属性.而且接口(Interface)不能实力化只能通过派生(

何双江(yarshray)的专栏 6491

C# 枚举、构造函数、析构函数、 重载、重写、 封装、继承、多态、集合、泛型、文件操作、委托与事件、进程与线程(未完成)

关于:枚举、构造函数、析构函数、C#方法的参数(实参和形参)、C# lambda表达式、递归和部分类 重载、重写、 封装、继承、多态、集合、泛型、文件操作、委托与事件、进程与线程的总结 ...

xu1993的博客 311

c#的多态和继承

01:什么是继承?代码举例说明 class Vhicel { private string _chelunshu; public string Chelunshu { get => _chelunshu; set => _chelunshu = value; } public void spend() { ...

qly11111111的博客 174

C#继承多态

01:什么是继承?代码举例说明。 就是子类继承父类,使子类具有父类的各种属性和方法。 //父类 public class student{ public int id { get; set; } public string name { get; set; } public Student(int id, string name) ...

qq_43583337的博客 171

c#面向对象--继承、多态

c#继承和多态学习记录

2301_76618920的博客 946
上一篇: (九)以交易为生:风险管理
下一篇: LeetCode 1470. 重新排列数组
coderpai
博客等级 码龄10年 1082粉丝 469原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值