Visual C++ .NET/Class/interface

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

base and interface

 
#include "stdafx.h"
using namespace System;
ref class MyClass{
   public:
      virtual void f() { Console::WriteLine("MyClass::f"); }
      virtual void g() { Console::WriteLine("MyClass::g"); }
};
interface class MyInterface{
   void f();
   void g();
};
ref class C : MyClass, MyInterface{
   public:
   virtual void f() new{
      Console::WriteLine("C::f");
   }
   virtual void g() override{
      Console::WriteLine("C::g");
   }
};
int main()
{
   MyClass^ b = gcnew MyClass();
   C^ c = gcnew C();
   MyInterface^ i = c;
   b->f(); // calls MyClass::f
   c->f(); // calls C::f
   i->f(); // calls C::f since C::f implements MyInterface::f
   MyClass^ bc = c;  // b pointing to instance of C
   bc->f(); // calls MyClass::f since C::f is unrelated
   b->g();  // calls MyClass::g
   c->g();  // calls C::g
   i->g();  // calls C::g since C::g implements MyInterface::g
   bc->g(); // calls C::g since C::g overrides MyClass::g
}


class interface method ambiguity

 
#include "stdafx.h"
using namespace System;
interface class MyInterface{
   void f();
};
ref class MyClass : MyInterface{
   public:
   void f()
   {
     Console::WriteLine("MyClass::f");
   }
   virtual void fMyInterface() = MyInterface::f
   {
     Console::WriteLine("MyClass::fMyInterface implementing MyInterface::f");
   }
};
int main()
{
   MyClass^ a = gcnew MyClass();
   MyInterface^ ia = a;
   ia->f();
   a->f();
}


constants in interfaces

 
#include "stdafx.h"
interface class I
{
   static const int i = 100;     // OK : static members OK
   literal int j = 50;           // OK : literals OK
};


Explicit interface implementation

 
#include "stdafx.h"
using namespace System;
interface class MyInterface1 { void f(); };
interface class MyInterface2 { void f(); };
ref class MyClass : MyInterface1, MyInterface2
{
   public:
   virtual void f1() = MyInterface1::f
   {
      Console::WriteLine("MyClass::f1 == MyInterface1::f");
   }
   virtual void f2() = MyInterface2::f
   {
      Console::WriteLine("MyClass::f2 == MyInterface2::f");
   }
};
int main()
{
   MyClass^ r = gcnew MyClass();
   MyInterface1^ i1 = r;
   MyInterface2^ i2 = r;
   r->f1();       // OK -- call through the object.
   r->f2();       // OK -- call through the object.

   i1->f();       // OK -- call f1.
   i2->f();       // OK -- call f2.
}


Interface and class(The virtual keyword is required to implement the interface method)

 
#include "stdafx.h"
interface class IInterface
{
   void f();
   int g();
};
ref class MyClass : IInterface
{
   public:
      virtual void f() { }
      virtual int g() { return 1; }
};


Interface definition

 
#include "stdafx.h"
using namespace System;
interface class Interface1
{
    void Method1();
    void Method2();
};
interface class Interface2
{
    void Method3();
    property String^ X;
};
ref class Base
{
public:
    void MethodBase()
    {
        Console::WriteLine("MethodBase()");
    }
};
ref class DerivedClass : public Base, public Interface1, public Interface2
{
public:
    virtual property String^ X
    {
        String^ get()
        {
            return x;
        }
    
        void  set(String^ value)
        {
            x = value;
        }
    }
    virtual void Method1()
    {
        Console::WriteLine("Method1()");
    }
    virtual void Method2()
    {
        Console::WriteLine("Method2()");
    }
    virtual void Method3()
    {
        Console::WriteLine("Method3()");
    }
    virtual void Print()
    {
        MethodBase();
        Method1();
        Method2();
        Method3();
    }
private:
    String^ x;
};
void main()
{
    DerivedClass dc;
    dc.X = "asdf";
    Console::WriteLine(dc.X);
    dc.Print();
}


Interface list

 
#include "stdafx.h"
using namespace System;
interface class MyInterface {};
interface class MyInterfaceB {};
ref class Base : MyInterface   // OK
{ };
ref class Derived : Base, MyInterface  // OK : Base class first
{ };
ref class A : Object, MyInterface  // OK: Object may be explicitly stated.
{ };
value class V : ValueType, MyInterface  // OK: Value class inherits from ValueType.
{ };
ref class B : MyInterfaceB, Base  // OK. Base class need not appear first (as in C#).
{ };


Interface name collision

 
#include "stdafx.h"
using namespace System;
interface class MyInterface1 { void f(); };
interface class MyInterface2 { void f(); };
ref class MyClass : MyInterface1, MyInterface2
{
   public:
   virtual void f()
   { Console::WriteLine("MyClass::f"); }
};
int main()
{
   MyClass^ r = gcnew MyClass();
   r->f();  // MyClass::f() implements both MyInterface1"s f and MyInterface2"s f
}


Interface properties events

 
#include "stdafx.h"
using namespace System;
interface class MyInterface
{
   property int P1;
   event EventHandler^ E;
   property int P2
   {
      int get();
      void set(int v);
   }
};
ref class MyClass : MyInterface
{
   int value;
   public:
   virtual property int P1;
   virtual event EventHandler^ E;
   virtual property int P2
   {
      int get() { return value; }
      void set(int v) { value = v; }
   }
};


interfaces implementing interfaces

 
#include "stdafx.h"
interface class MyInterface { void f(); };
interface class IB : MyInterface { void g(); };
ref class MyClass : IB
{
   public:
      virtual void f() {}
      virtual void g() {}
};


multiple interfaces

 
interface class MyInterface { void f(); };
interface class IB { void g(); };
ref class MyClass : MyInterface, IB
{
   public:
      virtual void f() {}
      virtual void g() {}
};


Private interface

 
#include "stdafx.h"
interface class IInterface
{
   void f();
   int g();
};
ref class MyClass : IInterface
{
      virtual void f() sealed = IInterface::f
      { }
   public:
      virtual int g() { return 1; }
};
int main()
{
   MyClass^ r = gcnew MyClass();
   IInterface^ ir = r;
   ir->f();  // f may be called through the interface.
   r->g();     // OK
}


static interfaces

 
#include "stdafx.h"
using namespace System;
interface class MyInterface
{
   static int i = 6;
   static const int j = 100;
   static void f()  { Console::WriteLine("MyInterface::f " + i); }
};
ref class A : MyInterface
{
   public:
   static void f() { Console::WriteLine("A::f " + MyInterface::j); }
};
int main()
{
   A^ a = gcnew A();
   MyInterface^ ia = a;
   ia->f();    
   a->f();     
   MyInterface::f();
   A::f();      
   a->MyInterface::f();
}