如何:定义接口静态构造函数 (C++/CLI)

接口可具有静态构造函数,可以将初始化静态数据成员。首次静态的接口成员,访问静态构造函数是最多调用一次和以前调用。

有关更多信息,请参见如何:在类或结构中定义静态构造函数

示例

// mcppv2_interface_class2.cpp
// compile with: /clr
using namespace System;

interface struct MyInterface {
   static int i;
   static void Test() {
      Console::WriteLine(i);
   }

   static MyInterface() { 
      Console::WriteLine("in MyInterface static constructor");
      i = 99;
   }
};

ref class MyClass : public MyInterface {};

int main() {
   MyInterface::Test();
   MyClass::MyInterface::Test();

   MyInterface ^ mi = gcnew MyClass;
   mi->Test();
}
  

请参见

参考

接口类