需要帮助理解实现接口的抽象类

时间:2021-10-27 02:00:46

Consider the following example. I have an interface MyInterface, and then two abstract classes MyAbstractClass1 and MyAbstractClass2. MyAbstractClass1 implements MyInterface, but MyAbstractClass2 does not.

请考虑以下示例。我有一个接口MyInterface,然后是两个抽象类MyAbstractClass1和MyAbstractClass2。 MyAbstractClass1实现MyInterface,但MyAbstractClass2没有。

Now I have three concrete classes.

现在我有三个具体的课程。

  1. MyConcreteClass1 is derived from MyAbstractClass1 but does not implement MyInterface.
  2. MyConcreteClass1派生自MyAbstractClass1,但未实现MyInterface。

  3. MyConcreteClass2 is derived from MyAbstractClass2, but does implement MyInterface.
  4. MyConcreteClass2派生自MyAbstractClass2,但确实实现了MyInterface。

  5. MyConcreteClass3 is derived from MyAbstractClass1, and does implement MyInterface.
  6. MyConcreteClass3派生自MyAbstractClass1,并实现MyInterface。

Does ConcreteClass1 also implicitly implement MyInterface because it derives from MyAbstractClass1? Assuming MyAbstractClass1 implements the methods of MyInteface implicitly then ConcreteClass1 should not have to be cast to a MyInterface to access the MyInteface methods right?

ConcreteClass1是否也隐式实现MyInterface,因为它派生自MyAbstractClass1?假设MyAbstractClass1隐式实现MyInteface的方法,那么不应该将ConcreteClass1强制转换为MyInterface来访问MyInteface方法吗?

MyAbstractClass1 can implicitly implement a method of MyInterface as an abstract method, but can't explicitly implement a method of MyInterface as an abstract method. Why is this?

MyAbstractClass1可以隐式地将MyInterface的方法实现为抽象方法,但不能将MyInterface的方法显式实现为抽象方法。为什么是这样?

Is MyConcreteClass3 excessive because it's implementing an interface that is already implemented by its base class? Would there be a reason you would want to do that even if you knew all classes that derive from MyAbstractClass1 should also implement MyInterface.

MyConcreteClass3是否过度,因为它正在实现一个已经由其基类实现的接口?即使您知道从MyAbstractClass1派生的所有类也应该实现MyInterface,您是否有理由想要这样做?

Here's a class diagram

这是一个类图

alt text http://files.getdropbox.com/u/113068/abstractclassesandinterfaces.png

alt text http://files.getdropbox.com/u/113068/abstractclassesandinterfaces.png

Here's the code:

这是代码:

//interface
public interface MyInterface
{
    void MyMethodA();
    void MyMethodB();
    void MyMethodC();

}

//abstract classes
public abstract class MyAbstractClass1 : MyInterface
{
    public void MyMethodA()
    {
    }


    void MyInterface.MyMethodB()
    {

    }

    //Error: "the modifier abstract is not valid for this item"
    //abstract void MyInterface.MyMethodC();

    //This works
    public abstract void MyMethodC();

    public abstract void MyMethodZ();

}


public abstract class MyAbstractClass2
{
    public void MyMethodX()
    {
    }

    public abstract void MyMethodY();

}

//Concrete classes
//ConcreteClass 1: Only Abstract class implements the interface
public class ConcreteClass1 : MyAbstractClass1
{
    public override void MyMethodC()
    {
    }

    public override void MyMethodZ()
    {

    }
}

//ConcreteClass 1: Only Concrete class implements the interface
public class ConcreteClass2 : MyAbstractClass2, MyInterface
{
    public override void MyMethodY()
    {
    }

    public void MyMethodA()
    {

    }

    public void MyMethodB()
    {

    }

    public void MyMethodC()
    {

    }

}
//ConcreteClass 1: Both concrete and abstract class implement the interface
public class ConcreteClass3 : MyAbstractClass1, MyInterface
{
    public override void MyMethodC()
    {

    }

    public override void MyMethodZ()
    {

    }
}

3 个解决方案

#1


Does ConcreteClass1 also implicitly implement MyInterface because it derives from MyAbstractClass1?

ConcreteClass1是否也隐式实现MyInterface,因为它派生自MyAbstractClass1?

Yes.

ConcreteClass1 should not have to be cast to a MyInterface to access the MyInteface methods right?

不应该将ConcreteClass1强制转换为MyInterface来访问MyInteface方法吗?

Correct. (myConcreteClass1 is MyInterface) will evaluate true.

正确。 (myConcreteClass1是MyInterface)将评估为true。

MyAbstractClass1 can implicitly implement a method of MyInterface as an abstract method, but can't explicitly implement a method of MyInterface as an abstract method.

MyAbstractClass1可以隐式地将MyInterface的方法实现为抽象方法,但不能将MyInterface的方法显式实现为抽象方法。

Explicit implementation is to distinguish between overlapping member signatures. The explicit implementation is private to the class you are implementing it on, so it is not accessible to derived classes (and thus cannot be abstract). You also cannot force classes which derive from MyAbstractClass1 to explicitly implement MyInterface, so there is no way to ensure the abstract member will ever be implemented.

明确的实现是区分重叠的成员签名。显式实现对于您正在实现它的类是私有的,因此派生类无法访问它(因此不能是抽象的)。您也不能强制从MyAbstractClass1派生的类显式实现MyInterface,因此无法确保抽象成员将被实现。

Is MyConcreteClass3 excessive because it's implementing an interface that is already implemented by its base class? Would there be a reason you would want to do that even if you knew all classes that derive from MyAbstractClass1 should also implement MyInterface.

MyConcreteClass3是否过度,因为它正在实现一个已经由其基类实现的接口?即使您知道从MyAbstractClass1派生的所有类也应该实现MyInterface,您是否有理由想要这样做?

Not necessarily, If you need to explicitly implement a member of the interface to distinguish it from an overlapping member on MyConcreteClass3. Otherwise it is unnecessary.

不一定,如果您需要显式实现接口的成员,以区别于MyConcreteClass3上的重叠成员。否则就没必要了。

#2


In this case, all three classes implement the interface (directly or indirectly). This is because MyAbstractClass1 implements MyInterface, and since MyConcreteClass1 derives from MyAbstractClass1, it also follows that you can treat MyConcreteClass1 as a MyInterface. MyConcreteClass2 can be treated with something that derives from MyAbstractClass1, as long as you treat it as a MyInterface. the derivation from MyInterface in ConcreteClass3 is a bit redundant since MyAbstractClass1 already implements MyInterface.

在这种情况下,所有三个类都实现接口(直接或间接)。这是因为MyAbstractClass1实现了MyInterface,并且由于MyConcreteClass1派生自MyAbstractClass1,因此您可以将MyConcreteClass1视为MyInterface。 MyConcreteClass2可以使用派生自MyAbstractClass1的东西来处理,只要将其视为MyInterface即可。 ConcreteClass3中MyInterface的派生有点多余,因为MyAbstractClass1已经实现了MyInterface。

With all of that information, i'd say that yes, it is redundant to implement MyInterface on MyConcreteClass3 since it derives from MyAbstractClass1 which already implements MyInterface. I think the reason that you cant have an abstract implementation of an interface method is that it provides no code itself and you cannot guarantee that it will be overriden in subcalsses. Use Virtual instead.

有了所有这些信息,我会说是的,在MyConcreteClass3上实现MyInterface是多余的,因为它派生自已实现MyInterface的MyAbstractClass1。我认为你不能有一个接口方法的抽象实现的原因是它本身不提供代码,你不能保证它将在子类中被覆盖。请改用Virtual。

#3


It's not redundant. Consider this class setup to simplify ...

这不是多余的。考虑这个类设置来简化......

public interface I
{
    void A();
}

public abstract class B : I
{
    public void A( )
    {
        Console.WriteLine("Base");
    }
}

public class D : B
{
    public void A()
    {
        Console.WriteLine("Hide");
    }
}

public class U
{
    public void M(I i)
    {
        Console.WriteLine("M!");
    }
}

Executing this ...

执行这个......

var d = new D();
var i = (I)d;
var u = new U();

i.A();
d.A();
u.M(d);
u.M(i);

You will get ...

你会得到 ...

Base
Hide
M!
M!

If you add the interface from the derived class ...

如果从派生类添加接口...

public class D : B, I
{
  public void A()
  {
    Console.WriteLine("Hide");
  }
}

You will get ...

你会得到 ...

Hide
Hide
M!
M!

So, it effects which implementation of the interface method you get when you get the Iness of your derived class.

因此,当您获得派生类的Iness时,它会影响您获得的接口方法的实现。

#1


Does ConcreteClass1 also implicitly implement MyInterface because it derives from MyAbstractClass1?

ConcreteClass1是否也隐式实现MyInterface,因为它派生自MyAbstractClass1?

Yes.

ConcreteClass1 should not have to be cast to a MyInterface to access the MyInteface methods right?

不应该将ConcreteClass1强制转换为MyInterface来访问MyInteface方法吗?

Correct. (myConcreteClass1 is MyInterface) will evaluate true.

正确。 (myConcreteClass1是MyInterface)将评估为true。

MyAbstractClass1 can implicitly implement a method of MyInterface as an abstract method, but can't explicitly implement a method of MyInterface as an abstract method.

MyAbstractClass1可以隐式地将MyInterface的方法实现为抽象方法,但不能将MyInterface的方法显式实现为抽象方法。

Explicit implementation is to distinguish between overlapping member signatures. The explicit implementation is private to the class you are implementing it on, so it is not accessible to derived classes (and thus cannot be abstract). You also cannot force classes which derive from MyAbstractClass1 to explicitly implement MyInterface, so there is no way to ensure the abstract member will ever be implemented.

明确的实现是区分重叠的成员签名。显式实现对于您正在实现它的类是私有的,因此派生类无法访问它(因此不能是抽象的)。您也不能强制从MyAbstractClass1派生的类显式实现MyInterface,因此无法确保抽象成员将被实现。

Is MyConcreteClass3 excessive because it's implementing an interface that is already implemented by its base class? Would there be a reason you would want to do that even if you knew all classes that derive from MyAbstractClass1 should also implement MyInterface.

MyConcreteClass3是否过度,因为它正在实现一个已经由其基类实现的接口?即使您知道从MyAbstractClass1派生的所有类也应该实现MyInterface,您是否有理由想要这样做?

Not necessarily, If you need to explicitly implement a member of the interface to distinguish it from an overlapping member on MyConcreteClass3. Otherwise it is unnecessary.

不一定,如果您需要显式实现接口的成员,以区别于MyConcreteClass3上的重叠成员。否则就没必要了。

#2


In this case, all three classes implement the interface (directly or indirectly). This is because MyAbstractClass1 implements MyInterface, and since MyConcreteClass1 derives from MyAbstractClass1, it also follows that you can treat MyConcreteClass1 as a MyInterface. MyConcreteClass2 can be treated with something that derives from MyAbstractClass1, as long as you treat it as a MyInterface. the derivation from MyInterface in ConcreteClass3 is a bit redundant since MyAbstractClass1 already implements MyInterface.

在这种情况下,所有三个类都实现接口(直接或间接)。这是因为MyAbstractClass1实现了MyInterface,并且由于MyConcreteClass1派生自MyAbstractClass1,因此您可以将MyConcreteClass1视为MyInterface。 MyConcreteClass2可以使用派生自MyAbstractClass1的东西来处理,只要将其视为MyInterface即可。 ConcreteClass3中MyInterface的派生有点多余,因为MyAbstractClass1已经实现了MyInterface。

With all of that information, i'd say that yes, it is redundant to implement MyInterface on MyConcreteClass3 since it derives from MyAbstractClass1 which already implements MyInterface. I think the reason that you cant have an abstract implementation of an interface method is that it provides no code itself and you cannot guarantee that it will be overriden in subcalsses. Use Virtual instead.

有了所有这些信息,我会说是的,在MyConcreteClass3上实现MyInterface是多余的,因为它派生自已实现MyInterface的MyAbstractClass1。我认为你不能有一个接口方法的抽象实现的原因是它本身不提供代码,你不能保证它将在子类中被覆盖。请改用Virtual。

#3


It's not redundant. Consider this class setup to simplify ...

这不是多余的。考虑这个类设置来简化......

public interface I
{
    void A();
}

public abstract class B : I
{
    public void A( )
    {
        Console.WriteLine("Base");
    }
}

public class D : B
{
    public void A()
    {
        Console.WriteLine("Hide");
    }
}

public class U
{
    public void M(I i)
    {
        Console.WriteLine("M!");
    }
}

Executing this ...

执行这个......

var d = new D();
var i = (I)d;
var u = new U();

i.A();
d.A();
u.M(d);
u.M(i);

You will get ...

你会得到 ...

Base
Hide
M!
M!

If you add the interface from the derived class ...

如果从派生类添加接口...

public class D : B, I
{
  public void A()
  {
    Console.WriteLine("Hide");
  }
}

You will get ...

你会得到 ...

Hide
Hide
M!
M!

So, it effects which implementation of the interface method you get when you get the Iness of your derived class.

因此,当您获得派生类的Iness时,它会影响您获得的接口方法的实现。