Define class implementations in an interface

Can you define class implementations in an interface?

For example (warning about pseudo-code!) ...

interface IClass1
{
    String s { get; set; }

    // classes implementing this interface has to implement Class2 as "SubClass"
    Class2 SubClass;
}

interface IClass2
{
     Int32 i { get; set; }
}

class Class1 : IClass1
{
    String IClass1.s { get; set; }

    class IClass1.Class2 SubClass
    {
        Int32 IClass2.i { get; set; }
    }
}
+5
source share
5 answers

The purpose of the interface is to define a contract that is separate from any implementation .

What you can do with an interface is a property definition :

interface IClass1
{
    String S { get; set; }

    Class2 SubClass { get; set; }
}
+4
source

. , Class2 , . "" - ( ) , "" ( Java super, base #, ). "" "" - #, , , ++, , , , , ( , ).

, .

+3

. , , , IClass1 Class2.

, :

  • Class2 , .
  • SubClass IClass1 , .
+2

, , , , (VB 2013 .NET 4.0 4.5). , , , . , , / , . (/ , , ). VB #.

VB:

Interface IPrintable
    Property Body As DocBody

    Class DocBody
        Property Text As String
        Property FontSize As Single
    End Class
End Interface

Class WordDoc
    Implements IPrintable
    Public Property WordBody As IPrintable.DocBody Implements IPrintable.Body
End Class

#:

interface IPrintable
{
    DocBody Body { get; set; }
    public class DocBody
    {
        public string Text { get; set; }
        public float FontSize { get; set; }
    }
}

class WordDoc : IPrintable
{
    public IPrintable.DocBody WordBody { get; set; }
    IPrintable.DocBody IPrintable.Body {
        get { return WordBody; }
        set { WordBody = value; }
    }
}
+2

, : " ?"

" /".
"" , , .

, .

, , , , . .
CLR , . .

. , , . , , , , , ...

, , , #, , , , , ++, " , .

( , ).
, , , .

, , ...

+1

All Articles