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

青岛如何做网站seowordpress卸载

青岛如何做网站seo,wordpress卸载,游戏开发和网站开发那个有前景,自己做网站需要服务器吗现在你对继承的基本语法已经比较熟悉了,是时候探索继承是c语言中重要属性的一个主要原因了。继承是一个装备允许你平衡既有代码。本节会举出基于代码重用目的的继承的例子。 1、WeatherPrediction类 假想你有一个任务,写一个程序来发出简单的天气预报&a…

        现在你对继承的基本语法已经比较熟悉了,是时候探索继承是c++语言中重要属性的一个主要原因了。继承是一个装备允许你平衡既有代码。本节会举出基于代码重用目的的继承的例子。

1、WeatherPrediction类

        假想你有一个任务,写一个程序来发出简单的天气预报,要用华氏度与摄氏度。作为程序员天气预报可能有一点儿超出你的领域,所以包含一个基于当前温度与当前金星与火星之间的距离写来用于天气预报的第三方的类库。第三方包用编译好的库进行分发,是为了保护预报算法的知识产权,但是你可以看到类定义。weather_prediction模块接口文件如下:

export module weather_prediction;import std;// Predicts the weather using proven new-age techniques given the current
// temperature and the distance from Jupiter to Mars. If these values are
// not provided, a guess is still given but it's only 99% accurate.
export class WeatherPrediction
{
public:// Virtual destructorvirtual ~WeatherPrediction();// Sets the current temperature in Fahrenheitvirtual void setCurrentTempFahrenheit(int temp);// Sets the current distance between Jupiter and Marsvirtual void setPositionOfJupiter(int distanceFromMars);// Gets the prediction for tomorrow's temperaturevirtual int getTomorrowTempFahrenheit() const;// Gets the probability of rain tomorrow. 1 means// definite rain. 0 means no chance of rain.virtual double getChanceOfRain() const;// Displays the result to the user in this format:// Result: x.xx chance. Temp. xxvirtual void showResult() const;// Returns a string representation of the temperaturevirtual std::string getTemperature() const;private:int m_currentTempFahrenheit{ 0 };int m_distanceFromMars{ 0 };
};

        注意该类将所有的成员函数都标识为virtual,因为该类假定它们会在继承类中被重载。

        该类解决了程序的大部分问题。然而,通常情况下就是这样,这并没有准确地完成你的需求。首先,所有的温度都是以华氏度给的。你和应用程序需要以摄氏度运行。还有,showResult()成员函数可能不是你想要的结果显示的方式。

2、在继承类中添加功能

        在学习继承时,添加功能是描述的第一个技巧。本质上,你的程序需要的就是像WeatherPrediction类但是还要有一些额外的华丽的点缀。听起来像是一个重用代码的继承的好的案例。我们先开始定义一个新的类,MyWeatherPrediction,它继承自WeatherPrediction:

import weather_prediction;
export class MyWeatherPrediction : public WeatherPrediction
{
};

        前面的类定义编译没有问题,MyWeatherPrediction类可以用于WeatherPrediction的位置。它提供了同样的功能,但是还没有任何新意。第一次修改,你可能想添加类的摄氏度的知识。你可能有一点儿左右为难,因为你不知道类内部是怎么做的。如果所有的内部计算都用的是华氏的,你怎么加上摄氏度的支持呢?一个方法是使用继承类做为中间商,在用户之间交互,谁能用什么度,基类呢,只能理解华氏度。

        支持摄氏度的第一步就是添加成员函数允许客户设置以摄氏度而不是华氏度的当前温度,来得到明天的预报,以摄氏度而不是华氏度。你还需要私有的辅助函数来双向转换摄氏度与华氏度。这些函数可以是静态的,因为对于类实例是一样的。

export class MyWeatherPrediction : public WeatherPrediction
{
public:virtual void setCurrentTempCelsius(int temp);virtual int getTomorrowTempCelsius() const;
private:static int convertCelsiusToFahrenheit(int celsius);static int convertFahrenheitToCelsius(int fahrenheit);
};

        新的成员函数与父类的命名规则保持一致。记住从其它代码的角度,MyWeatherPrediction对象有所有的定义在MyWeatherPrediction与WeatherPrediction中的功能。适应父类的命名规范提供了一致的接口。

        华氏度与摄氏度的转换留给大家去做吧,实在做不来,上网上搜一下也能获取!另两个成员函数就比较有趣了。用摄氏度设置当着温度,需要首先转换温度,然后用它能理解的单位给到父类:

void MyWeatherPrediction::setCurrentTempCelsius(int temp)
{int fahrenheitTemp{ convertCelsiusToFahrenheit(temp) };setCurrentTempFahrenheit(fahrenheitTemp);
}

        可以看到,一旦温度被转换,就可以调用基类的既有的功能。同样的,getTomorrowTempCelsius()的实现使用父类既有的功能去获得华氏度温度,但是在返回之前转换一下结果:

int MyWeatherPrediction::getTomorrowTempCelsius() const
{int fahrenheitTemp{ getTomorrowTempFahrenheit() };return convertFahrenheitToCelsius(fahrenheitTemp);
}

        两个新的成员函数高效重用了父类,因为它们“打包”既有的功能,用一种提供了新的接口的方式来使用它。

        也可以添加新功能与父类既有的功能毫无关系。例如,可以添加一个成员函数来从互联网上访问另外的预报,或者一个成员函数基于预报的天气建议相关活动。

3、在继承类中替换功能

        继承的另外的主要技术是替换既有的功能。在WeatherPrediction类中的showResult()成员函数急需整容。MyWeatherPrediction可以重载成员函数来用它自己的实现替换它的行为。

新的MyWeatherPredition类定义如下:

export class MyWeatherPrediction : public WeatherPrediction
{
public:virtual void setCurrentTempCelsius(int temp);virtual int getTomorrowTempCelsius() const;void showResult() const override;private:static int convertCelsiusToFahrenheit(int celsius);static int convertFahrenheitToCelsius(int fahrenheit);
};

        下面是一个新的,用户更友好的重载showResult()成员函数的实现:

void MyWeatherPrediction::showResult() const
{println("Tomorrow will be {} degrees Celsius ({} degrees Fahrenheit)",getTomorrowTempCelsius(), getTomorrowTempFahrenheit());println("Chance of rain is {}%", getChanceOfRain() * 100);if (getChanceOfRain() > 0.5) {println("Bring an umbrella!");}
}

        对于使用这个类的客户,就像旧版本的showResult()从来没有存在过。只要对象是MyWeatherPrediction,就会调用新的版本。作为这些改变的结果,MyWeatherPrediction作为一个带有新功能适应更确定的目的的新类出现。但是,它并不需要太多的代码,因为它利用了基类的既有的功能。

http://www.yayakq.cn/news/257123/

相关文章:

  • intitle 做网站会员管理系统手机版
  • 创建网站的向导和模板手机网站跳转怎么办
  • 交通运输企业标准建设网站企业网站开发基本流程
  • 如何制作和设计公司网站制作网页前为什么要建立站点
  • 本地广东中山网站建设山东官网建设公司
  • 威海市建设局官方网站深圳网页设计兴田德润优惠吗
  • 做网站用vue还是用jquery常州网站建设系统
  • 做一个商城网站多少钱福步外贸论坛app
  • 西宁seo网站网站策划书籍推荐
  • 壹搜网站建设下载页面设计图片
  • 建站服务论坛杭州编程培训机构排名
  • 网站建设中国十强图书馆建设投稿网站
  • 中国建设质量网官方网站食品公司
  • 网站的网页邗江区做网站
  • 温州哪里有网站建设网站怎样注册
  • 深圳网站的网络公司开发商不按时交房可以退房吗
  • 国外虚拟物品交易网站sdk直播
  • 惠州网站建设公司哪家好wordpress 文章下载
  • 网站建设界面ppt演示ideas wordpress theme 2.0
  • 怎么做自己的店铺网站如何用VS2017做网站
  • 个体户可以网站备案吗网站建设指导合同
  • 网站做专业团队公司网页设计内容方案
  • 商务卫士包括网站建设产品网站开发流程
  • 网站开发最适合语言武进网站建设价位
  • 布吉网站建设价格前端主要做什么
  • 色和尙做爰网站网站源码带采集
  • 网站广告条素材做娱乐自媒体有哪些网站可以推荐
  • 网站制作厦门公司搜搜提交网站
  • 大型电子商务网站建设成本网站正在建设页面
  • 电商网站对比网站后台模板怎样使用