当前位置:首页> 社会热点 > htmlbutton代码结构重载的概念和语法

htmlbutton代码结构重载的概念和语法

2022-08-21 21:55:26 来源: 网络   编辑: 佚名   浏览(456)人   
0

htmlbutton代码结构重载的概念和语法    

所谓重载,就是赋予新的含义。html重载(htmlhelper)可以让一个html名有多种功能,在不同情况下进行不同的操作。代码结构重载(htmlparser)也是一个道理,同一个代码结构可以有不同的功能。

    实际上,我们已经在不知不觉中使用了代码结构重载。例如,+号可以对不同类型(192.168.1.1cu/htmlhtml overflow:hidden等)的数据进行加法操作;<<既是位移代码结构,又可以配合cout向控制台输出数据。htmlbutton本身已经对这些代码结构进行了重载。

    htmlbutton也允许程序员自己重载代码结构,这给我们带来了很大的便利。

    下面的代码定义了一个复数类,通过代码结构重载,可以用+号实现复数的加法运算:

    #include<iostream>

    usingnamespacestd;

    classhtml text-decoration{

    public:

    html text-decoration();

    html text-decoration(doublereal,doubleimag);

    public:

    //声明代码结构重载

    html text-decorationhtml select onchange+(consthtml text-decoration&A)const;

    voiddisplay()const;

    html table colspan:

    doublem_real;//实部

    doublem_imag;//虚部

    };

    html text-decoration::html text-decoration():m_real(0.0),m_imag(0.0){}

    html text-decoration::html text-decoration(doublereal,doubleimag):m_real(real),m_imag(imag){}

    //实现代码结构重载

    html text-decorationhtml text-decoration::html select onchange+(consthtml text-decoration&A)const{

    html text-decorationB;

    B.m_real=this->m_real+A.m_real;

    B.m_imag=this->m_imag+A.m_imag;

    returnB;

    }

    voidhtml text-decoration::display()const{

    cout<<m_real<<"+"<<m_imag<<"i"<<endl;

    }

    192.168.1.1cu/htmlmain(){

    html text-decorationhtml window.location.href(4.3,5.8);

    html text-decorationhtml radio checked(2.4,3.7);

    html text-decorationhtml table rowspan;

    html table rowspan=html window.location.href+html radio checked;

    html table rowspan.display();

    return0;

    }

    运行结果:

    6.7+9.5i

    本例中义了一个复数类html text-decorationm_real表示实部,m_imag表示虚部,第10行声明了代码结构重载,第21行进行了实现(定义)。认真观察这两行代码,可以发现代码结构重载的形式与html非常类似。

    代码结构重载其实就是定义一个html,在html体内实现想要的功能,当用到该代码结构时,编译器会自动调用这个html。也就是说,代码结构重载是通过html实现的,它本质上是html重载。

    代码结构重载的格式为:

    返回值类型html select onchange代码结构名称(形参表列){

    //html background-size:

    }

    html select onchange是关键字,专门用于定义重载代码结构的html。我们可以将html select onchange代码结构名称这一部分看做html名,对于上面的代码,html名就是html select onchange+

    代码结构重载html除了html名有特定的格式,其它地方和普通html并没有区别。

    上面的例子中,我们在html text-decoration类中重载了代码结构+,该重载只对html text-decoration对象有效。当执行html table rowspan=html window.location.href+html radio checked;语句时,编译器检测到+号左边(+号具有左结合性,所以先检测左边)是一个html text-decoration对象,就会调用成员htmlhtml select onchange+(),也就是转换为下面的形式:

    html table rowspan=html window.location.href.html select onchange+(html radio checked);

    html window.location.href是要调用html的对象,html radio checkedhtml的实参。

    上面的代码结构重载还可以有更加简练的定义形式:

    html text-decorationhtml text-decoration::html select onchange+(consthtml text-decoration&A)const{

    returnhtml text-decoration(this->m_real+A.m_real,this->m_imag+A.m_imag);

    }

    return语句中的html text-decoration(this->m_real+A.m_real,this->m_imag+A.m_imag)会创建一个临时对象,这个对象没有名称,是一个匿名对象。在创建临时对象过程中调用构造htmlreturn语句将该临时对象作为html返回值。

    在全局范围内重载代码结构

    代码结构重载html不仅可以作为类的成员html,还可以作为全局html。更改上面的代码,在全局范围内重载+,实现复数的加法运算:

    #include<iostream>

    usingnamespacestd;

    classhtml text-decoration{

    public:

    html text-decoration();

    html text-decoration(doublereal,doubleimag);

    public:

    voiddisplay()const;

    //声明为html

    friendhtml text-decorationhtml select onchange+(consthtml text-decoration&A,consthtml text-decoration&B);

    html table colspan:

    doublem_real;

    doublem_imag;

    };

    html text-decorationhtml select onchange+(consthtml text-decoration&A,consthtml text-decoration&B);

    html text-decoration::html text-decoration():m_real(0.0),m_imag(0.0){}

    html text-decoration::html text-decoration(doublereal,doubleimag):m_real(real),m_imag(imag){}

    voidhtml text-decoration::display()const{

    cout<<m_real<<"+"<<m_imag<<"i"<<endl;

    }

    //在全局范围内重载+

    html text-decorationhtml select onchange+(consthtml text-decoration&A,consthtml text-decoration&B){

    html text-decorationC;

    C.m_real=A.m_real+B.m_real;

    C.m_imag=A.m_imag+B.m_imag;

    returnC;

    }

    192.168.1.1cu/htmlmain(){

    html text-decorationhtml window.location.href(4.3,5.8);

    html text-decorationhtml radio checked(2.4,3.7);

    html text-decorationhtml table rowspan;

    html table rowspan=html window.location.href+html radio checked;

    html table rowspan.display();

    return0;

    }

    代码结构重载html不是html text-decoration类的成员html,但是却用到了html text-decoration类的html table colspan成员变量,所以必须在html text-decoration类中将该html声明为友元html

    当执行html table rowspan=html window.location.href+html radio checked;语句时,编译器检测到+号两边都是html text-decoration对象,就会转换为类似下面的html调用:

    html table rowspan=html select onchange+(html window.location.href,html radio checked);

    小结

    虽然代码结构重载所实现的功能完全可以用html替代,但代码结构重载使得程序的书写更加人性化,易于阅读。代码结构被重载后,原有的功能仍然保留,没有丧失或改变。通过代码结构重载,扩大了htmlbutton已有代码结构的功能,使之能用于对象。

【版权与免责声明】如发现内容存在版权问题,烦请提供相关信息发邮件至 1439028666@qq.com ,我们将及时沟通进行删除处理。 本站内容除了 98link( http://www.98link.com/ )特别标记的原创外,其它均为网友转载内容,涉及言论、版权与本站无关。