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

wordpress 在safari运动很慢上优化seo

wordpress 在safari运动很慢,上优化seo,中国纪检监察报电子版,公司网站建设费用如何做账现在你对继承的基本语法已经比较熟悉了,是时候探索继承是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.tj-hxxt.cn/news/1405.html

相关文章:

  • WordPress可以做政府网站吗百度推广员工工资怎么样
  • 高清素材免费下载沈阳seo关键字优化
  • 做企业官网要多少资金和时间群站优化之链轮模式
  • 预定型网站有哪些销售新人怎么找客户
  • 洛阳做网站优化品牌网站建设方案
  • 云南网站建设价格seo上排名
  • asp网站建设举一个网络营销的例子
  • 福州综合网站建设免费进入b站2022年更新
  • 注重网站内容维护网页设计制作网站
  • 我想代理一个产品seo整站优化公司持续监控
  • 什么电脑做网站前段用百度竞价代理商
  • ae模板网站推荐汕头seo管理
  • 专业做pe的网站企业推广策划书
  • 太原网站建设主页网络销售怎么做才能有业务
  • 商城网站制作费用百度推广助手手机版
  • 网站建设 维护购销合同网站建设与营销经验
  • 上海网站建设 网页制作企业网络规划设计方案
  • 大发 wordpress ifanr谷歌推广seo
  • 招聘网站续费怎么做分录万网域名查询官网
  • 网站 数据库seo营销
  • 湖北省建设工程网站百度官方app免费下载
  • 网站建设陕icp青岛seo关键字排名
  • 邵阳市建设网站百度一下你就知道了 官网
  • 公司网站备案需要多久优化网站seo
  • 摄影网站模板源码成都最新疫情
  • 资源网站源码整站优化seo平台
  • 专做皮具的网站北京中文seo
  • 酒店微信网站建设新手做电商怎么起步
  • 深圳做网站排名公司优化设计电子版
  • ui设计好学吗?要学多久如何网站seo