NET类继承基类并实现接口问题

NET类继承基类并实现接口问题

本文介绍了VB.NET类继承基类并实现接口问题(在C#中有效)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在VB.NET中创建一个继承基本抽象类并实现接口的类。该接口声明了一个名为Description的字符串属性。基类包含一个名为Description的字符串属性。主类继承基类并实现接口。基类中存在Description属性可以满足接口要求。

I am trying to create a class in VB.NET which inherits a base abstract class and also implements an interface. The interface declares a string property called Description. The base class contains a string property called Description. The main class inherits the base class and implements the interface. The existence of the Description property in the base class fulfills the interface requirements. This works fine in C# but causes issues in VB.NET.

首先,这是一个有效的C#代码示例:

First, here is an example of the C# code which works:

public interface IFoo
{
    string Description { get; set; }
}

public abstract class FooBase
{
    public string Description { get; set; }
}

public class MyFoo : FooBase, IFoo
{
}

现在是VB.NET版本,它会产生编译器错误:

Now here is the VB.NET version which gives a compiler error:

Public Interface IFoo
    Property Description() As String
End Interface

Public MustInherit Class FooBase
    Private _Description As String
    Public Property Description() As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            _Description = value
        End Set
    End Property
End Class

Public Class MyFoo
    Inherits FooBase
    Implements IFoo
End Class

如果我使基类( FooBase )实现该接口并添加对属性实现IFoo.Description 很好,但是我不希望基类实现该接口。

If I make the base class (FooBase) implement the interface and add the Implements IFoo.Description to the property all is good, but I do not want the base class to implement the interface.

编译器错误是:

VB.NET不能处理此问题,还是我需要

Can VB.NET not handle this, or do I need to change my syntax somewhere to get this to work?

推荐答案

您需要将属性标记为 Overridable MustOverride ,然后可以在子类中覆盖它:

You need to mark your property as Overridable or MustOverride in the base class and then you can override it in the child class:

Public MustInherit Class FooBase
    Private _Description As String
    Public Overridable Property Description() As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            _Description = value
        End Set
    End Property
End Class

Public Class MyFoo
    Inherits FooBase
    Implements IFoo
    Public Overrides Property Description() As String Implements IFoo.Description
        Get
            Return MyBase.Description
        End Get
        Set(ByVal value As String)
            MyBase.Description = value
        End Set
    End Property
End Class

编辑
这是对@的回应嘛Hanin发布了。我们两种解决方案都可以工作,但是了解每种解决方案的重要性很重要。想象以下代码:

EditThis is in response to what @M.A. Hanin posted. Both of our solutions work but its important to understand the ramifications of each. Imagine the following code:

Dim X As FooBase = New MyFoo()
Trace.WriteLine(X.Description)

X.Description有什么用?使用Overridable,您将获得对子类的调用,而使用Overload方法,您将获得对基类的调用。对与错都不对,了解声明的后果很重要。使用Overload方法,您必须向上转换以获得孩子的实现:

What comes out of the X.Description? Using the Overridable you'll get the call to the child class while using the Overload method you'll get the call to the base class. Neither is right or wrong, its just important to understand the consequences of the declaration. Using the Overload method you have to up-cast to get the child's implementation:

Trace.WriteLine(DirectCast(X, MyFoo).Description)

如果您只是在调用MyBase.Description子类的问题尚无定论,但是如果您更改了子类的定义,则只需确保您了解正在发生的事情。

If you're just calling MyBase.Description from the child class the question is moot but if you ever change the definition of the child class then you just need to make sure you understand what's going on.

这篇关于VB.NET类继承基类并实现接口问题(在C#中有效)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:22