为什么我不能在接口上调用ToString()?

时间:2016-01-28 10:25:35

标签: c# vb.net

我在示例控制台应用中重新创建了这个问题,所以我很想知道发生了什么。

最初的问题是,在我的代码中,我有一个名为ICat的类,这个类是用C#编写的

public interface ICat
{
  string ToString(CatColour colour);
}

在同一个程序集中,在C#中有一个实现:

public class MagicCat : ICat
{
        public string ToString(CatColour colour)
        {
            return $"I am a {colour} cat";
        }
}

编译没有任何问题。

在另一个用VB.NET编写的程序集中,我有这段代码:

Dim myCat As ICat = GetCat()
Dim result = myCat.ToString() ' Error on this line

这会导致编译错误Argument not specified for parameter 'colour' of 'Function ToString(format As AddressFormat) As String'.

我尝试在C#应用中重新创建此代码,使用以下代码:

public class Cat : IAnimal
{
    public string ToString(CatColour colour)
    {
        return $"I am a {colour} cat.";
    }

    //public string ToString()
    //{
    //    return "I am a cat.";
    //}
}

public interface IAnimal
{
    string ToString(CatColour colour);
}

class Program
{
    static void Main(string[] args)
    {
        IAnimal cat = new Cat();

        Console.WriteLine(new Cat().ToString());
        Console.WriteLine(new Cat().ToString(CatColour.Red));

        Console.WriteLine(cat.ToString());
        Console.WriteLine(cat.ToString(CatColour.Blue));

        Console.ReadKey();
    }
}

public enum CatColour
{
    Red = 1,
    Blue = 2
}

它编译并运行,输出为:

  

ConsoleApplication1.Cat
  我是一只红猫   ConsoleApplication1.Cat
  我是一只蓝猫。

(如果我取消注释其他ToString()方法,则第一行是> I am a cat.

这就是我所期望的。

我将应用程序转换为VB.NET,期望得到上面的原始错误,但我得到了这个问题:

Public Class Cat
    Implements IAnimal
    Public Function ToString(colour As String) As String
        Return "I am a {colour} cat."
    End Function
End Class

Public Interface IAnimal
    Function ToString(colour As String) As String
End Interface

Class 'Cat' must implement 'Function ToString(colour As String) As String' for interface 'IAnimal'

那么这里发生了什么?为什么VB.NET给我的接口实现错误,为什么我的原始场景抱怨没有不带参数的ToString()方法?

<小时/> 编辑:我已将我的vb代码更新为:

Public Interface IAnimal
    Function ToString(colour As String) As String
End Interface

Public Class Cat
    Implements IAnimal
    Public Function ToString(colour As String) As String Implements IAnimal.ToString
        Return "I am a {colour} cat."
    End Function
End Class

Sub Main()

    Dim cat As Cat = New Cat()
    Dim icat As IAnimal = New Cat()

    Call cat.ToString()
End Sub

我得到Argument not specified for parameter 'colour' of 'Public Function ToString(colour As String) As String'这是原始问题,这在C#代码中不会发生。知道为什么吗? Cat是一个对象,因此有一个空白的ToString()方法。

2 个答案:

答案 0 :(得分:7)

您必须添加更多信息:

Public Class Cat
     Implements IAnimal
     Public OverLoads Function ToString(colour As String) As String Implements IAnimal.ToString
         Return $"I am a {colour} cat."
     End Function
End Class

Public Interface IAnimal
    Function ToString(colour As String) As String
End Interface

或者

Public Class Cat
     Implements IAnimal
     Public Function MyToString(colour As String) As String Implements IAnimal.ToString
         Return $"I am a {colour} cat."
     End Function
End Class

答案 1 :(得分:2)

您可能希望为Overloads带参数声明ToString()(因为您有另一个基础ToString()来自Object)和Overrides { {1}}没有您从ToString()派生的参数:

Object

或者,您可以为Public Class Cat Implements IAnimal Public Overloads Function ToString(colour As String) As String Implements IAnimal.ToString Return "I am a {colour} cat." End Function Public Overrides Function ToString() Return "I am a cat." End Function End Class Public Interface IAnimal Function ToString(colour As String) As String End Interface 创建两个ToString(),并在IAnimal而不是来自ToString()

的情况下覆盖IAnimal而无需参数
Object