21、面向对象编程:多态性与系统扩展性

面向对象编程:多态性与系统扩展性

1 引言

多态性是面向对象编程(OOP)的核心概念之一,它使得我们能够编写更加灵活和可扩展的程序。通过多态性,我们可以设计和实现易于扩展的系统——可以添加新的类,而对程序的通用部分几乎不需要或不需要任何修改。程序中必须改变以适应新类的部分,仅限于那些需要直接了解你添加到层次结构中的新类的部分。本文将深入探讨多态性的原理及其在实际编程中的应用,帮助读者更好地理解和运用这一强大工具。

2 多态性的基本概念

多态性允许我们在处理各种对象时使用一般化的接口,而具体的实现细节则由运行时环境处理。这意味着,只要对象属于同一个继承层次结构,并且通过共同的基类指针或基类引用来访问,就可以指示这些对象以适合各自类型的方式行为,而不必确切知道它们的具体类型。例如,假设我们有一个基类 Shape 和两个派生类 Circle 和 Rectangle ,我们可以通过 Shape* 或 Shape& 来调用这两个派生类的方法,而无需知道具体是哪个派生类的对象。

2.1 编写泛化代码

多态性使我们能够编写“泛化”的代码,而不是“具体化”的代码。这意味着我们可以在不知道具体对象类型的情况下,编写能够处理多种类型对象的代码。例如,我们可以编写一个函数来处理任何类型的 Shape 对象,而无需关心它是 Circle 还是 Rectangle 。

21天C#编程学习指南 C#作为微软开发的一种面向对象的高级编程语言,以其简洁、类型安全和强大的功能而著称。本章将带您走进C#的世界,从基础语法开始,逐步深入理解其核心特点和应用场景。类是一种定义状态(属性)和行为(方法)的模板或蓝图,它是创建对象的原型。在C#中,使用关键字class来定义一个类。set;set;上面的代码定义了一个Person类,包含两个属性Name和Age,以及一个方法Speak()。创建一个类的实例(对象)的过程叫做实例化。可以使用new关键字来创建一个类的实例。 阅读详情

相关推荐

班级管理系统源码深入解析与实践

Django是一个高层次的Python Web框架,它鼓励快速开发和干净、实用的设计。与Flask相比,Django更偏重于“约定优于配置”,自动处理很多Web开发中常见的任务,比如数据持久化和用户认证。初始化一个Django项目,通常会用到工具:这个命令会创建一个名为mysite的项目目录。进入项目目录后,可以运行开发服务器:Django项目的默认配置文件是。在实际开发中,经常需要修改一些基础配置,例如数据库配置、中间件、模板配置等。

weixin_32306683的博客 1085

Ubuntu 22.04下GNU Radio 3.10全家桶编译避坑实录:从UHD、VOLK到WiFi收发模块gr-ieee802-11

本文详细介绍了在Ubuntu 22.04系统下编译安装GNU Radio 3.10全家桶的完整流程,包括UHD驱动、VOLK优化库以及WiFi信号处理模块gr-ieee802-11的集成。通过逐步指导解决版本兼容性和依赖问题,帮助研究人员快速搭建高效的软件定义无线电开发环境,特别适合使用USRP设备进行无线通信研究的开发者。

weixin_30825199的博客 446

WinForm多语言切换实战:5分钟搞定中英文界面(附完整代码)

本文提供了WinForm应用实现中英文界面切换的两种高效实战方案。通过利用内置的本地化机制和资源文件,开发者可以在5分钟内为窗体添加多语言支持,并附有完整代码示例,帮助快速应对软件国际化需求。

weixin_29230773的博客 406

Databricks Genie:语义编译器架构与企业级智能解析实践

语义层是现代数据平台实现自然语言查询的核心基础,其本质是将业务术语映射为可执行、可治理的数据契约。原理上依赖结构化元数据、值字典与数据血缘三支柱协同,通过分层AI代理(如Intent Parsing、Retriever、Verifier)实现意图识别→语义对齐→安全执行的闭环。技术价值在于降低SQL使用门槛的同时保障审计性、一致性和权限可控性,适用于销售运营、财务分析等一线业务场景。本文深入解析Databricks Genie作为典型语义编译器的落地逻辑,涵盖Unity Catalog知识注入、七层Agen

weixin_29053695的博客 337

gesp c++ 五级知识点

gesp c++ 五级知识点

MMD.然大大 2007

V.PhyloMaker实战:从物种列表到维管束植物系统发育树的完整流程

本文详细介绍了使用V.PhyloMaker工具包,从物种列表快速构建维管束植物系统发育树的完整实战流程。内容涵盖R包安装、物种名标准化、核心函数phylo.maker的三种构建方案选择、结果树的可视化与导出,以及与picante包的联动分析,为生态学和生物地理学研究提供了高效解决方案。

weixin_29057237的博客 374

智能车赛道识别:八邻域算法的实战解析与优化

本文深入解析了智能车赛道识别中八邻域算法的核心原理与实战优化。作为兼顾性能与稳定性的经典方案,该算法通过模拟探险家环顾八方的策略,能精准追踪弯道与直道边界。文章详细拆解了代码逻辑,并提供了边界平滑、异常处理及应对十字路口、环岛等复杂场景的关键优化策略,助力开发者提升智能车视觉系统的鲁棒性与实时性。

weixin_29228781的博客 260

【WRF-Urban运行】WRF-SLUCM耦合建模参数设置及模拟运行

WRF-SLUCM耦合建模参数设置及模拟运行

WW、forever的博客 1723

2024届数字IC秋招-华为机试-数字芯片-笔试真题和答案(五)(含2022年和2023年)

此部分包含华为机试真题,方向是数字芯片,内容详细包括设计、验证、后端等,大部分题目给出参考答案。华为机试都是从题库里抽题目,所以这些题目还是很有参考性的,甚至是完全一样,可以搜到的。(题目内容较多,将分开编写几个文章)

qq_42135020的博客 2081

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

上一篇: 140、将程序划分为模块:提高代码的可维护性和可读性
下一篇: 64、智能交通与自动驾驶
raspberrypi5
博客等级 码龄1天 0粉丝 1856原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值