C # interface and inheritance issue

I am browsing C # (C ++ background) and I came to this piece of code:

public interface IUndoable { void Undo(); }
public class TextBox : IUndoable
{
    void IUndoable.Undo() { Console.WriteLine ("TextBox.Undo"); }
}

public class RichTextBox : TextBox, IUndoable
{
    public new void Undo() { Console.WriteLine ("RichTextBox.Undo"); }
}

Since a RichTextBox comes from a TextBox, can anyone explain why a RichTextBox also comes from IUndoable ?. I would have thought that the IUndoable interface would be “inherited” along with any other TextBox members that RichTextBox has access to?

As an aside, I guess from what I have read so far that the concept of open, protected, and private inheritance does not exist in C #.

Is this the right conclusion ?. If so, how will this behavior (i.e., inheritance restriction) be implemented in C #?

[change]

: , , - . , ( ) , , , (phew!).

:

:

public class RichTextBox : TextBox, IUndoable
{
    public new void Undo() { Console.WriteLine ("RichTextBox.Undo"); }
}

:

public class RichTextBox : TextBox
{
    public new void Undo() { Console.WriteLine ("RichTextBox.Undo"); }
}

, ( , ). , TextBox?

+5
6

# ++ .

. TextBox IUdoable,

void IUndoable.Undo() 

void Undo() 

TextBox. , :

TextBox tb = new TextBox();
tb.Undo(); // error
((IUndoable)tb).Undo(); // ok

, , "", RichTextBox IUndoable , :

RichTextBox rtb = new RichTextBox();
rtb.Undo(); // ok
+6

.

interface IUndoable
{
    void Undo();
}

class TextBox : IUndoable
{
    void IUndoable.Undo()
    {
        Console.WriteLine("TextBox.Undo");
    }
}

class RichTexBox : TextBox
{
    public new void Undo()
    {
        Console.WriteLine("RichTextBox.Undo");
    }
}

class FilthyRichTextBox : TextBox, IUndoable
{
    public new void Undo()
    {
        Console.WriteLine("FilthyRichTextBox.Undo");
    }
}

:

IUndoable text = new TextBox();
IUndoable richText = new RichTexBox();
IUndoable filthyRichText = new FilthyRichTextBox();

Console.WriteLine("From the TextBox:");
text.Undo();

Console.WriteLine("From the RichTextBox:");
richText.Undo();

Console.WriteLine("From the FilthyRichTextBox:");
filthyRichText.Undo();

:


:
TextBox.Undo
RichTextBox:
TextBox.Undo
FilthyRichTextBox:
FilthyRichTextBox.Undo

+2

RichTextBox IUndoable. ReSharper, , .

AFAIK , " ".

, , sealed.

+1

RichTextBox Undo ( ), , RichTextBox , , , , Undo TextBox . RichTextBox IUndoable , , - , IUndoable, . :

var obj=new RichTextBox();

    obj.Undo(); // hits the new method  
    ((TextBox)obj).Undo(); //hits the parent (old) method.
    ((IUndoable)obj).Undo(); //hits the new method if RichTextBox implements IUndoable and otherwise hits the old method

, Undo , , , , .

0

, ,

public class IUndoable
{
}
public class TextBox : IUndoable
{
   public new void Undo() { Console.WriteLine ("RichTextBox.Undo"); }
}

public class RichTextBox : TextBox
{
  public new void Undo() { Console.WriteLine ("RichTextBox.Undo"); }
}
0
source

You're right. No need to RichtTextBoximplement IUndoable. You can see that the compiler is also not very happy, since the method Undoin RichTextBoxhides the UndoTextBox method (see Keyword new).

The only reason a subclass needs to implement the interface of this base class is when these classes are ComVisible, and you want to also open this interface to COM.

0
source

All Articles