类实例化和构造函数

2024-09-01 03:12
文章标签 实例 构造函数

本文主要是介绍类实例化和构造函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

类实例化和构造函数

  • 类如何创立,如何调用构造函数
    • 源码
    • rv汇编
    • 行为分析
  • 一般成员函数
  • 虚函数
    • 源码
    • 汇编
    • 行为分析
  • 纯虚函数
    • 汇编
    • 行为分析
  • 多态
    • 源码
    • 汇编
    • 行为分析
  • 为什么构造函数不能是虚函数

类如何创立,如何调用构造函数

源码

#include <iostream>
using namespace std;// 抽象父类
class Base {int a,b;
public:Base(){a=1;b=2;}};// 子类
class Derived : public Base {
public:int c;Derived(){c=3;}
};int main() {Derived b ; return 0;
}

rv汇编

__cxx_global_var_init:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16lui     a0, %hi(std::__ioinit)addi    a0, a0, %lo(std::__ioinit)sw      a0, -12(s0)call    std::ios_base::Init::Init() [complete object constructor]lw      a1, -12(s0)lui     a0, %hi(_ZNSt8ios_base4InitD1Ev)addi    a0, a0, %lo(_ZNSt8ios_base4InitD1Ev)lui     a2, %hi(__dso_handle)addi    a2, a2, %lo(__dso_handle)call    __cxa_atexitlw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retmain:addi    sp, sp, -32sw      ra, 28(sp)sw      s0, 24(sp)addi    s0, sp, 32mv      a0, zerosw      a0, -28(s0)sw      a0, -12(s0)addi    a0, s0, -24call    Derived::Derived() [base object constructor]lw      a0, -28(s0)lw      s0, 24(sp)lw      ra, 28(sp)addi    sp, sp, 32retDerived::Derived() [base object constructor]:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)sw      a0, -16(s0)call    Base::Base() [base object constructor]lw      a1, -16(s0)addi    a0, zero, 3sw      a0, 8(a1)lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retBase::Base() [base object constructor]:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a1, -12(s0)addi    a0, zero, 1sw      a0, 0(a1)addi    a0, zero, 2sw      a0, 4(a1)lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16ret_GLOBAL__sub_I_example.cpp:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16call    __cxx_global_var_initlw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retstd::__ioinit:.zero   1

行为分析

  • 在main会去调用Derived的构造函数,并且在调用之前,会给a0寄存器一个地址,这个地址其实是this指针,也就是Derived实例化对象b的地址,main在传入之前已经为b实例开辟了栈空间
  • Derived的构造函数的也会传入b的地址给Base的构造函数
  • 进入Base构造就不难看出,这里拿着最开始b的地址在做实际的输出化了
        lw      a1, -12(s0)addi    a0, zero, 1sw      a0, 0(a1)addi    a0, zero, 2sw      a0, 4(a1)lw      s0, 8(sp)

这里是先把地址放在栈上,然后a1拿着地址,之后就往偏移位置放值,这里对应的就是初始化父类的成员变量

  • 调用完回到Derived的构造函数,再初始化子类成员变量
        lw      a1, -16(s0)addi    a0, zero, 3sw      a0, 8(a1)

一般成员函数

#include <iostream>
using namespace std;// 抽象父类
class Base {int a,b;
public:Base(){a=1;b=2;}};// 子类
class Derived : public Base {
public:int c;Derived(){c=3;}void show(){cout<<c<<endl;}
};int main() {Derived b ; b.show();return 0;
}

这里其实和构造函数一样,调用之气会传入变量的地址,也就是this指针,以便函数使用成员变量

虚函数

源码

#include <iostream>
using namespace std;class Base {
public:int a,b;Base(){a=1;b=2;}virtual void show1(){cout<<a<<endl;}virtual void show2(){cout<<a<<endl;}};// 子类
class Derived : public Base {
public:int c;Derived(){c=3;}virtual void show2(){cout<<a<<endl;}
};int main() {Derived b ; b.show1();return 0;
}

汇编

__cxx_global_var_init:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16lui     a0, %hi(std::__ioinit)addi    a0, a0, %lo(std::__ioinit)sw      a0, -12(s0)call    std::ios_base::Init::Init() [complete object constructor]lw      a1, -12(s0)lui     a0, %hi(_ZNSt8ios_base4InitD1Ev)addi    a0, a0, %lo(_ZNSt8ios_base4InitD1Ev)lui     a2, %hi(__dso_handle)addi    a2, a2, %lo(__dso_handle)call    __cxa_atexitlw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retmain:addi    sp, sp, -48sw      ra, 44(sp)sw      s0, 40(sp)addi    s0, sp, 48mv      a0, zerosw      a0, -36(s0)sw      a0, -12(s0)addi    a0, s0, -32sw      a0, -40(s0)call    Derived::Derived() [base object constructor]lw      a0, -40(s0)call    Base::show1()lw      a0, -36(s0)lw      s0, 40(sp)lw      ra, 44(sp)addi    sp, sp, 48retDerived::Derived() [base object constructor]:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)sw      a0, -16(s0)call    Base::Base() [base object constructor]lw      a1, -16(s0)lui     a0, %hi(vtable for Derived)addi    a0, a0, %lo(vtable for Derived)addi    a0, a0, 8sw      a0, 0(a1)addi    a0, zero, 3sw      a0, 12(a1)lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retBase::show1():addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)lw      a1, 4(a0)lui     a0, %hi(_ZSt4cout)addi    a0, a0, %lo(_ZSt4cout)call    std::ostream::operator<<(int)lui     a1, %hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)addi    a1, a1, %lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)call    std::ostream::operator<<(std::ostream& (*)(std::ostream&))lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retBase::Base() [base object constructor]:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a1, -12(s0)lui     a0, %hi(vtable for Base)addi    a0, a0, %lo(vtable for Base)addi    a0, a0, 8sw      a0, 0(a1)addi    a0, zero, 1sw      a0, 4(a1)addi    a0, zero, 2sw      a0, 8(a1)lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retDerived::show2():addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)lw      a1, 4(a0)lui     a0, %hi(_ZSt4cout)addi    a0, a0, %lo(_ZSt4cout)call    std::ostream::operator<<(int)lui     a1, %hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)addi    a1, a1, %lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)call    std::ostream::operator<<(std::ostream& (*)(std::ostream&))lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retBase::show2():addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)lw      a1, 4(a0)lui     a0, %hi(_ZSt4cout)addi    a0, a0, %lo(_ZSt4cout)call    std::ostream::operator<<(int)lui     a1, %hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)addi    a1, a1, %lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)call    std::ostream::operator<<(std::ostream& (*)(std::ostream&))lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16ret_GLOBAL__sub_I_example.cpp:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16call    __cxx_global_var_initlw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retstd::__ioinit:.zero   1vtable for Derived:.word   0.word   typeinfo for Derived.word   Base::show1().word   Derived::show2()typeinfo name for Derived:.asciz  "7Derived"typeinfo name for Base:.asciz  "4Base"typeinfo for Base:.word   _ZTVN10__cxxabiv117__class_type_infoE+8.word   typeinfo name for Basetypeinfo for Derived:.word   _ZTVN10__cxxabiv120__si_class_type_infoE+8.word   typeinfo name for Derived.word   typeinfo for Basevtable for Base:.word   0.word   typeinfo for Base.word   Base::show1().word   Base::show2()

行为分析

有虚函数的话,对象的首地址就会设置为虚函数表的位置,然后父类设置之后子类再设置覆盖掉。

纯虚函数

源码

#include <iostream>
using namespace std;class Base {
public:int a,b;Base(){a=1;b=2;}virtual void show1(){cout<<a<<endl;}virtual void show2()=0;};// 子类
class Derived : public Base {
public:int c;Derived(){c=3;}virtual void show2(){cout<<a<<endl;}
};int main() {Derived b ; b.show1();return 0;
}

汇编

__cxx_global_var_init:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16lui     a0, %hi(std::__ioinit)addi    a0, a0, %lo(std::__ioinit)sw      a0, -12(s0)call    std::ios_base::Init::Init() [complete object constructor]lw      a1, -12(s0)lui     a0, %hi(_ZNSt8ios_base4InitD1Ev)addi    a0, a0, %lo(_ZNSt8ios_base4InitD1Ev)lui     a2, %hi(__dso_handle)addi    a2, a2, %lo(__dso_handle)call    __cxa_atexitlw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retmain:addi    sp, sp, -48sw      ra, 44(sp)sw      s0, 40(sp)addi    s0, sp, 48mv      a0, zerosw      a0, -36(s0)sw      a0, -12(s0)addi    a0, s0, -32sw      a0, -40(s0)call    Derived::Derived() [base object constructor]lw      a0, -40(s0)call    Base::show1()lw      a0, -36(s0)lw      s0, 40(sp)lw      ra, 44(sp)addi    sp, sp, 48retDerived::Derived() [base object constructor]:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)sw      a0, -16(s0)call    Base::Base() [base object constructor]lw      a1, -16(s0)lui     a0, %hi(vtable for Derived)addi    a0, a0, %lo(vtable for Derived)addi    a0, a0, 8sw      a0, 0(a1)addi    a0, zero, 3sw      a0, 12(a1)lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retBase::show1():addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)lw      a1, 4(a0)lui     a0, %hi(_ZSt4cout)addi    a0, a0, %lo(_ZSt4cout)call    std::ostream::operator<<(int)lui     a1, %hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)addi    a1, a1, %lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)call    std::ostream::operator<<(std::ostream& (*)(std::ostream&))lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retBase::Base() [base object constructor]:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a1, -12(s0)lui     a0, %hi(vtable for Base)addi    a0, a0, %lo(vtable for Base)addi    a0, a0, 8sw      a0, 0(a1)addi    a0, zero, 1sw      a0, 4(a1)addi    a0, zero, 2sw      a0, 8(a1)lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retDerived::show2():addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)lw      a1, 4(a0)lui     a0, %hi(_ZSt4cout)addi    a0, a0, %lo(_ZSt4cout)call    std::ostream::operator<<(int)lui     a1, %hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)addi    a1, a1, %lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)call    std::ostream::operator<<(std::ostream& (*)(std::ostream&))lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16ret_GLOBAL__sub_I_example.cpp:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16call    __cxx_global_var_initlw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retstd::__ioinit:.zero   1vtable for Derived:.word   0.word   typeinfo for Derived.word   Base::show1().word   Derived::show2()typeinfo name for Derived:.asciz  "7Derived"typeinfo name for Base:.asciz  "4Base"typeinfo for Base:.word   _ZTVN10__cxxabiv117__class_type_infoE+8.word   typeinfo name for Basetypeinfo for Derived:.word   _ZTVN10__cxxabiv120__si_class_type_infoE+8.word   typeinfo name for Derived.word   typeinfo for Basevtable for Base:.word   0.word   typeinfo for Base.word   Base::show1().word   __cxa_pure_virtual

行为分析

抽象类的虚函数表里标注了纯虚函数,这里是找不到base的show2实现的

vtable for Base:.word   0.word   typeinfo for Base.word   Base::show1().word   __cxa_pure_virtual

行为和没有纯虚函数一样,也是替换表

多态

源码

#include <iostream>
using namespace std;class Base {
public:int a,b;Base(){a=1;b=2;}virtual void show2(){cout<<a<<endl;}virtual void show1(){cout<<a<<endl;}};// 子类
class Derived : public Base {
public:int c;Derived(){c=3;}virtual void show2(){cout<<a<<endl;}virtual void show3(){cout<<a<<endl;}
};// 修正后的函数
void callShow2(Base* x){x->show2();
}int main() {Derived b ; b.show1();callShow2(&b); // 调用修正后的函数来展示 show2 的效果return 0;
}

汇编

__cxx_global_var_init:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16lui     a0, %hi(std::__ioinit)addi    a0, a0, %lo(std::__ioinit)sw      a0, -12(s0)call    std::ios_base::Init::Init() [complete object constructor]lw      a1, -12(s0)lui     a0, %hi(_ZNSt8ios_base4InitD1Ev)addi    a0, a0, %lo(_ZNSt8ios_base4InitD1Ev)lui     a2, %hi(__dso_handle)addi    a2, a2, %lo(__dso_handle)call    __cxa_atexitlw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retcallShow2(Base*):addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)lw      a1, 0(a0)lw      a1, 0(a1)jalr    a1lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retmain:addi    sp, sp, -48sw      ra, 44(sp)sw      s0, 40(sp)addi    s0, sp, 48mv      a0, zerosw      a0, -36(s0)sw      a0, -12(s0)addi    a0, s0, -32sw      a0, -40(s0)call    Derived::Derived() [base object constructor]lw      a0, -40(s0)call    Base::show1()lw      a0, -40(s0)call    callShow2(Base*)lw      a0, -36(s0)lw      s0, 40(sp)lw      ra, 44(sp)addi    sp, sp, 48retDerived::Derived() [base object constructor]:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)sw      a0, -16(s0)call    Base::Base() [base object constructor]lw      a1, -16(s0)lui     a0, %hi(vtable for Derived)addi    a0, a0, %lo(vtable for Derived)addi    a0, a0, 8sw      a0, 0(a1)addi    a0, zero, 3sw      a0, 12(a1)lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retBase::show1():addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)lw      a1, 4(a0)lui     a0, %hi(_ZSt4cout)addi    a0, a0, %lo(_ZSt4cout)call    std::ostream::operator<<(int)lui     a1, %hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)addi    a1, a1, %lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)call    std::ostream::operator<<(std::ostream& (*)(std::ostream&))lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retBase::Base() [base object constructor]:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a1, -12(s0)lui     a0, %hi(vtable for Base)addi    a0, a0, %lo(vtable for Base)addi    a0, a0, 8sw      a0, 0(a1)addi    a0, zero, 1sw      a0, 4(a1)addi    a0, zero, 2sw      a0, 8(a1)lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retDerived::show2():addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)lw      a1, 4(a0)lui     a0, %hi(_ZSt4cout)addi    a0, a0, %lo(_ZSt4cout)call    std::ostream::operator<<(int)lui     a1, %hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)addi    a1, a1, %lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)call    std::ostream::operator<<(std::ostream& (*)(std::ostream&))lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retDerived::show3():addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)lw      a1, 4(a0)lui     a0, %hi(_ZSt4cout)addi    a0, a0, %lo(_ZSt4cout)call    std::ostream::operator<<(int)lui     a1, %hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)addi    a1, a1, %lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)call    std::ostream::operator<<(std::ostream& (*)(std::ostream&))lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retBase::show2():addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16sw      a0, -12(s0)lw      a0, -12(s0)lw      a1, 4(a0)lui     a0, %hi(_ZSt4cout)addi    a0, a0, %lo(_ZSt4cout)call    std::ostream::operator<<(int)lui     a1, %hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)addi    a1, a1, %lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)call    std::ostream::operator<<(std::ostream& (*)(std::ostream&))lw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16ret_GLOBAL__sub_I_example.cpp:addi    sp, sp, -16sw      ra, 12(sp)sw      s0, 8(sp)addi    s0, sp, 16call    __cxx_global_var_initlw      s0, 8(sp)lw      ra, 12(sp)addi    sp, sp, 16retstd::__ioinit:.zero   1vtable for Derived:.word   0.word   typeinfo for Derived.word   Derived::show2().word   Base::show1().word   Derived::show3()typeinfo name for Derived:.asciz  "7Derived"typeinfo name for Base:.asciz  "4Base"typeinfo for Base:.word   _ZTVN10__cxxabiv117__class_type_infoE+8.word   typeinfo name for Basetypeinfo for Derived:.word   _ZTVN10__cxxabiv120__si_class_type_infoE+8.word   typeinfo name for Derived.word   typeinfo for Basevtable for Base:.word   0.word   typeinfo for Base.word   Base::show2().word   Base::show1()

行为分析

首先观察表,子类是肯定包含了父类的所有虚函数的,只是会把重新实现的,替换为自己的,所以体现多态就是这样,我们传入参数是基类指针,因为默认的虚函数表是在首地址的,大家都一样,然后我们都想调用show2(),因为穿进来的指针指向的地址拿到的虚函数表是不一样的,虽然偏移一样,但是最后调用的就是不同的虚函数实现。

为什么构造函数不能是虚函数

由之前的行为分析知道,子类实例化过程中,会去调用父类的构造函数,将对象的首地址设置为父类的虚函数表,再去设置父类的成员变量,再退回到子类构造函数重复一样的操作。
对于虚函数,是为了覆盖父类的行为实现多态,那么假设构造是虚函数,怎么去覆盖父类的构造行为,虚函数的;
进一步将,子类的行为是,调用父类构造,然后覆盖他的虚函数表,然后父类构造变成了子类构造,这完全乱套。
所以来说,构造根本就和多态不是一个东西,混在一起完全没有意义,他只是帮助设置初值和虚函数表的,与多态行为无关。

这篇关于类实例化和构造函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1125784

相关文章

java图像识别工具类(ImageRecognitionUtils)使用实例详解

《java图像识别工具类(ImageRecognitionUtils)使用实例详解》:本文主要介绍如何在Java中使用OpenCV进行图像识别,包括图像加载、预处理、分类、人脸检测和特征提取等步骤... 目录前言1. 图像识别的背景与作用2. 设计目标3. 项目依赖4. 设计与实现 ImageRecogni

Java操作ElasticSearch的实例详解

《Java操作ElasticSearch的实例详解》Elasticsearch是一个分布式的搜索和分析引擎,广泛用于全文搜索、日志分析等场景,本文将介绍如何在Java应用中使用Elastics... 目录简介环境准备1. 安装 Elasticsearch2. 添加依赖连接 Elasticsearch1. 创

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

Oracle Expdp按条件导出指定表数据的方法实例

《OracleExpdp按条件导出指定表数据的方法实例》:本文主要介绍Oracle的expdp数据泵方式导出特定机构和时间范围的数据,并通过parfile文件进行条件限制和配置,文中通过代码介绍... 目录1.场景描述 2.方案分析3.实验验证 3.1 parfile文件3.2 expdp命令导出4.总结

MySQL的索引失效的原因实例及解决方案

《MySQL的索引失效的原因实例及解决方案》这篇文章主要讨论了MySQL索引失效的常见原因及其解决方案,它涵盖了数据类型不匹配、隐式转换、函数或表达式、范围查询、LIKE查询、OR条件、全表扫描、索引... 目录1. 数据类型不匹配2. 隐式转换3. 函数或表达式4. 范围查询之后的列5. like 查询6

Python开发围棋游戏的实例代码(实现全部功能)

《Python开发围棋游戏的实例代码(实现全部功能)》围棋是一种古老而复杂的策略棋类游戏,起源于中国,已有超过2500年的历史,本文介绍了如何用Python开发一个简单的围棋游戏,实例代码涵盖了游戏的... 目录1. 围棋游戏概述1.1 游戏规则1.2 游戏设计思路2. 环境准备3. 创建棋盘3.1 棋盘类

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

C++操作符重载实例(独立函数)

C++操作符重载实例,我们把坐标值CVector的加法进行重载,计算c3=c1+c2时,也就是计算x3=x1+x2,y3=y1+y2,今天我们以独立函数的方式重载操作符+(加号),以下是C++代码: c1802.cpp源代码: D:\YcjWork\CppTour>vim c1802.cpp #include <iostream>using namespace std;/*** 以独立函数

实例:如何统计当前主机的连接状态和连接数

统计当前主机的连接状态和连接数 在 Linux 中,可使用 ss 命令来查看主机的网络连接状态。以下是统计当前主机连接状态和连接主机数量的具体操作。 1. 统计当前主机的连接状态 使用 ss 命令结合 grep、cut、sort 和 uniq 命令来统计当前主机的 TCP 连接状态。 ss -nta | grep -v '^State' | cut -d " " -f 1 | sort |

Java Websocket实例【服务端与客户端实现全双工通讯】

Java Websocket实例【服务端与客户端实现全双工通讯】 现很多网站为了实现即时通讯,所用的技术都是轮询(polling)。轮询是在特定的的时间间隔(如每1秒),由浏览器对服务器发 出HTTP request,然后由服务器返回最新的数据给客服端的浏览器。这种传统的HTTP request 的模式带来很明显的缺点 – 浏 览器需要不断的向服务器发出请求,然而HTTP