在.net 应用程序设计中如何选择class, abstract class ...

.NET Interface [接口]、abstract【抽象类】,接口中可以包含什么? 1、抽象方法只作声明,而不包含实现,可以看成是没有实现的虚方法2、抽象类可以但不是必须有抽象属性、抽象方法,但是一旦有了抽象方法,就一定要把这个类 声明成 抽象类3、抽象类不能实例化4、具体派生类 必须 覆盖基类的抽象方法5、抽象派送类可以覆盖基类的抽象方法,也可以不覆盖,则其具体实现派生类不行覆盖它们。//属性 public string Name {get;set;} //虚方法 public virtual string getName() { 阅读详情

本文面向的读者:.net 应用程序设计师和程序员

关键字:

type� 类型

class - 类

abstract - 抽象的

interface - 接口

member - 成员

method - 方法

property - 属性

预备知识:在阅读本文时,您应当了解.net编程的基本知识并且已经掌握class, abstract class 和 interface全部知识。这里我仅简单介绍一下他们的基本知识。本文的例子由c#编写。期望您对c#编程有一定的了解。

正文:

我们无法创建一个abstract class或interface的实例(instance)。让我们从abstract class和interface的定义来看他们的不同。abstract class可以包含abstract methods 和 abstract properties, 也可以包含其他的members,象正常的class一样。而interface只能包含abstract methods和properties(属性)。interface中的所有methods和properties不需要加abstract和public关键字,因为这两个关键字在interface中是默认的。举例如下:

class则可以提供更好的灵活性。你可以给class添加任何members,只要添加的不是abstract method即可(也就是说你要提供一个有具体实现的方法)。这样就不会影响从该class继承的类,已有代码无需做任何改变。

设计原则

• 优先考虑使用class或abstract class而不是interface。

• 使用abstract class代替interface来降低class继承层次之间的耦合关系。

• 使用interface,如果你需要给一个值类型实现(value type, 象struct就是值类型)多态继承(polymorphic hierarchy)。(值类型除了从interface继承以外,不能从其他type继承)。

• 在需要多重继承的情况下,可以考虑使用interface。

参考目录:microsoft .net development series, framework design guidelines

(责任编辑:徐培炎)


.NET抽象类 Abstract 虚方法 Virtural 接口 interface 因为抽象方法声明不提供实际的实现,所以没有方法体;虚方法必须有实现部分,并为派生类提供了重写该方法的选项,相反,抽象方法没有提供实现部分,强制派生类覆盖方法(否则派生类不能成为具体类);被子类重写的方法,如果子类重写了虚方法,那么运行时将使用重写后的逻辑,如果没有重写,则使用父类中虚方法的逻辑;虚方法必须有实现部分,抽象方法没有提供实现部分,抽象方法是一种强制派生类覆盖的方法,否则派生类将不能被实例化。(8) 抽象类实现的具体方法默认为虚的,但实现接口的类中的接口方法却默认为非虚的,当然您也可以声明为。.. 阅读详情

相关推荐

asp.net中virtual和abstract的区别分析

本文实例分析了asp.net中virtual和abstract的区别,分享给大家供大家参考。具体分析如下: 一、Virtual方法(虚方法) virtual 关键字用于在基类中修饰方法。virtual的使用会有两种情况: 情况1:在基类中定义了virtual方法,但在派生类中没有重写该虚方法。那么在对派生类实例的调用中,该虚方法使用的是基类定义的方法。 情况2:在基类中定义了virtual方法,然后在派生类中使用override重写该方法。那么在对派生类实例的调用中,该虚方法使用的是派生重写的方法。 当一个方法被声明为Virtual时,它是一个虚拟方法,直到你使用ClassName varia

AD怎么设置相同网络的线宽

一、对网络进行分类。快捷键“D+C”。在弹出的面板中找到“net class”,单击右键,选择“Add Class” 二、选择网络。选中Net Classes中刚刚建好的类POWER,按住CTRL键,在Non-Members中选择5V、20V(先选中所需网络名,滚动鼠标中间键,可以查看选择其它网络),然后点击“>"图标,这些网络就会Members下面的框框中。“OK”确定退出。...

随风@飘扬 8194

【阅读文献笔记】FoundationStereo: Zero-Shot Stereo Matching

提出首个零样本泛化立体匹配基础模型设计 STA 模块融合单目先验提出 AHCF 模块增强代价滤波构建 100 万合成数据集 FSD + 自净化流程。

qq_46460379的博客 1258

.NET 中的抽象类和抽象方法

如果一个类不与具体的事物相联系,而只是表达一种抽象的概念,仅仅是作为其派生类的一个基类,这样的类就是抽象类,在抽象类中声明方法时,如果加上abstract时就是抽象方法。4:抽象类中的成员可以私有的(只要不是抽象的)、受保护的、内部的或受保护的内部成员,但接口中的成员必须是公共的。1:它们的派生类只能继承一个基类,即只能继承一个抽象类,但是可以继承多个接口。2:抽象类中可以包含抽象成员,但非抽象类中不可以。2:抽象类中可以定义成员的实现,但接口中不可以。1:抽象方法必须声明在抽象类中。

学IT的人 714

Ubuntu下为RK3568开发板搭建Qt交叉编译环境:手把手教你配置Qt Creator和远程调试

本文详细介绍了在Ubuntu系统下为RK3568开发板搭建Qt交叉编译环境的完整流程,包括工具链配置、Qt源码交叉编译、Qt Creator环境设置以及远程调试技巧。通过手把手教程,开发者可以快速掌握RK3568平台的Qt开发环境搭建方法,实现高效的应用开发和部署。

weixin_30776273的博客 375

ZedBoard+Vivado(二)——PS + PL实现流水灯

硬件:Zedboard 软件:Vivado2018.2 + Win10 本文参考了http://blog.chinaaet.com/cuter521/p/35978的设计 1 设计 功能:PS + PL流水灯 语言:C 流程:创建工程->IP Integrator->综合、实现、生成Bitstream->SDK 2 流程 2.1 创建工程 跟上一篇步骤完全相同,这里不用添加源文...

q416524389的博客 3701

.NET里面 abstract class和Interface有什么区别以及用法的展现?

前言 很多新人对这个问题已经看到的厌倦或者是恶心了,有可能是因为比较难理解或者是未理解的情况下对它们的认知不够所以产生的想法是差不多,不用抽奖类和接口我一样能实现我想要的功能。 这句话确实不假,但是实现的功能是否符合面向对象思想就不得而知了,也有很多的朋友知道它们的意思但是许久不用也忘记的差不多了,那么我们今天就在来巩固下 抽象类和接口的一些区别。 为什么频频出现这个问题,无论是在面试过程中...

weixin_30731305的博客 279

QCC308X/518X/309X加LDAC解码

QCC308X/518X/309X加LDAC解码。

Alex_886的博客 6612

在.NET 应用程序设计中如何选择Class, Abstract Class and Interface

本文面向的读者:.NET 应用程序设计师和程序员 关键字: Type– 类型 Class - 类 Abstract - 抽象的 Interface - 接口 Member - 成员 Method - 方法 Property - 属性 预备知识:在阅读本文时,您应当了解.NET编程的基本知识并且已经掌握Class, Abstract Class 和 Int...

weixin_30830327的博客 74

STM32F103 CAN通信避坑指南:从标准库配置到收发测试的完整流程

本文详细介绍了STM32F103 CAN通信的实现流程,从标准库配置到收发测试,帮助开发者避开常见陷阱。内容涵盖硬件连接、波特率计算、过滤器配置及错误处理等关键环节,提供实用的代码示例和调试技巧,确保CAN通信的稳定性和可靠性。

weixin_30627341的博客 325

华为防火墙NAT避坑指南:当双ISP出口遇到服务器发布,这些细节不注意会翻车

本文深入解析华为防火墙在双ISP出口环境下服务器发布的实战配置,重点探讨no-reverse参数对Server-Map表的影响、源进源出功能的实现及线路切换时的会话保持问题。通过策略路由和智能选路技术,确保HTTP/FTP服务在多运营商链路中的高可用性,避免路径不对称和NAT映射冲突等常见陷阱。

weixin_30897233的博客 550

C#面试理论+笔试题整理(带答案)

一、C# 理论 1.1、简述 private、 protected、 public、 internal、protected internal访问修饰符和访问权限 private :私有成员, 在类的内部才可以访问。 protected :保护成员,该类内部和继承类中可以访问。 public :公共成员,完全公开,没有访问限制。 internal:当前程序集内可以访问。 pro...

MintYouth的博客 1万+

18、Excel VBA编程:自定义函数创建与代码调试全攻略

本文详细介绍了Excel VBA中自定义函数的创建方法,以获取单元格注释为例演示了完整开发流程,并深入讲解了代码调试的重要性与核心工具。内容涵盖三种主要错误类型及其应对策略,系统介绍了调试工具栏、断点、逐行执行、Locals窗口、Immediate窗口等实用功能。通过综合案例展示了如何运用调试技术解决实际问题,并总结了提前规划、逐步测试、日志记录等最佳实践,帮助读者提升VBA编程效率与代码质量。

life6的博客 101

理解abstract class和interface

abstract class和interface是Java语言中对于抽象类定义进行支持的两种机制,正是由于这两种机制的存在,才赋予了Java强大的面向对象能力。abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性,甚至可以相互替换,因此很多开发者在进行抽象类定义时对于abstract class和interface的选择显得比较随意。其实,两者之间还是有很

段sir 的专栏 590

基于STM32、HAL库、I2C接口的姿态传感器/陀螺仪之ICM-42670-P驱动 C语言程序设计

/ 设置加速度计和陀螺仪的工作模式。// 读取加速度计和陀螺仪数据。

corlin6688的博客 1182

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

abstract class

声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用 于要创建一个体现某些基本行为的类,并为该类声明方法,但不能在该类中实现 该类的情况。不能创建abstract 类的实例。然而可以创建一个变量,其类型是 一个抽象类,并让它指向具体子类的一个实例。不能有抽象构造函数或抽象静态 方法。Abstract 类的子类为它们父类中的所有抽象方法提供实...

175

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

2021-05-16 C#.NET面试题 C#中 abstract class和interface有什么区别?

C#中 abstract class和interface有什么区别? abstract class abstract 声明抽象类抽象方法,一个类中有抽象方法,那么这个类就是抽象类了。所谓的抽象方法,就是不含主体(不提供实现方法),必须由继承者重写。因此,抽象类不可实例化,只能通过继承被子类重写。 interface 声明接口,只提供一些方法规约,在C#8之前的版本中不提供任何实现,在C#9版本也可以支持接口的实现;不能用public、abstract等修饰,无字段、常量,无构造函数 两者区别: 1.inte

微软MVP Eleven 6万+

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

深度学习工具包 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")

接口(interface)vs. 抽象类(abstract class)

本文内容 概述 设计理念 应用场合 interface vs. abstract class 修改记录   概述 网上大多数资料,在比较 interface 和 abstract class 区别时,往往是先从语法,然后实现(编程),最后是设计理念和应用场合。我觉得这样不妥!设计理念才决定了,它们在语法、编程和应用上的差异。 另外,作为 C# 程序员的我,开始会忘记——继承

yo548720570的专栏 743

深入理解abstract class和interface

理解抽象类 abstract class和interface在Java语言中都是用来进行抽象类定义的,那么什么是抽象类,使用抽象类能给我们带来什么好处呢? 在面向对象的概念中,我们知道所有的对象都是通过类来描述的,但是反过来却不是这样。并不是所有的类都是用来描述对象的,入过一个类中没有包含足够的信息来描述一个具体的对象,这样的类就是抽象类。抽象类往往用来表征我们再对问题领域进行分析、设计中得出的抽...

weixin_43842753的博客 319

abstract class和interface有什么区别?_走进C# abstract,了解抽象类与接口的异同

思维导航:前言abstract修饰符抽象类与接口的异同抽象类和接口的使用总结参考资料文章正文:?回到顶部前言:  最近在研究.NET Core的一些类库源码的时候发现在底层类库中使用了大量的抽象类,然而发现自己搬砖这么多年好像从来没有在项目中使用抽象类或者抽象方法去实现某个功能模块,并且对修饰符Abstract概念也只懂得一些皮毛(也就是面试的时候会问下其基本作用)。当然,咱们不懂就要去...

weixin_33438741的博客 356

c# interface 和 abstract class的区别

Difference Between C# Interface vs Abstract Class C# is anobject-oriented programming languagethat is used in .Net framework to develop the desktop applications and web applications as well. It was...

taoqick的专栏 926

深入理解abstract class和interface (转)

深入理解abstract class和interface (转)[@more@] ChinaJava/l-javainterface-abstract/index.shtml#main">ip to main cont...

csd3176的博客 113

深入理解abstract class和interface 选择自 legendinfo 的 Blog 原文地址:http://dev.csdn.net/develop/article/58/5...

abstract class和interface是Java语言中对于抽象类定义进行支持的两种机制,正是由于这两种机制的存在,才赋予了Java强大的面向对象能力。abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性,甚至可以相互替换,因此很多开发者在进行抽象类定义时对于abstract class和interface的选择显得比较随意。其实,两者之间还是有很...

weixin_30955617的博客 96

C# 虚方法(virtual)与抽象方法(abstract)对比:3个核心差异与5种适用场景

本文深入解析C#中虚方法(virtual)与抽象方法(abstract)的核心差异与应用场景,涵盖语法对比、运行时行为机制及5种实战应用场景。通过具体代码示例,帮助开发者理解如何在插件系统、模板方法模式等领域灵活运用这两种多态实现方式,提升面向对象设计能力。

weixin_34211761的博客 506
上一篇: .net多线程编程(3):线程同步
下一篇: 2008新春巨献,动网.net论坛v1.1 rc1发布
Labber
博客等级 码龄19年 26粉丝 1976原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值