C# 将派生类作为参数传递给用于基类参数的方法-获取错误

C# 将派生类作为参数传递给用于基类参数的方法-获取错误,c#,inheritance,C#,Inheritance,在有人指出之前,我已经读过了,运气不好 我有一个LatLon类,它扩展了Bing.Maps的Location类(),主要是重写GetHashCode和Equals方法来执行值相等,而不是内置的引用相等,如下所示: class LatLon : Location { public readonly double Latitude; public readonly double Longitude; public LatLon(double

在有人指出之前,我已经读过了,运气不好

我有一个LatLon类,它扩展了Bing.Maps的Location类(),主要是重写GetHashCode和Equals方法来执行值相等,而不是内置的引用相等,如下所示:

class LatLon : Location
    {
        public readonly double Latitude;
        public readonly double Longitude;

        public LatLon(double Latitude, double Longitude)
        {
            this.Latitude = Latitude;
            this.Longitude = Longitude;
        }

        public override bool Equals(System.Object obj)
        {
            // If parameter is null return false.
            if (obj == null)
            {
                return false;
            }

            // If parameter cannot be cast to Point return false.
            LatLon p = obj as LatLon;
            if ((System.Object)p == null)
            {
                return false;
            }

            // Return true if the fields match:
            return (Latitude == p.Latitude) && (Longitude == p.Longitude);
        }

        public bool Equals(LatLon p)
        {
            // If parameter is null return false:
            if ((object)p == null)
            {
                return false;
            }

            // Return true if the fields match:
            return (Latitude == p.Latitude) && (Longitude == p.Longitude);
        }

        public override int GetHashCode()
        {
            int hash = 13;
            hash = (hash * 92821) + Latitude.GetHashCode();
            hash = (hash * 92821) + Longitude.GetHashCode();
            return hash;
        }
    }
在另一节课上,我试着做以下几点:

LatLon l1 = (LatLon) getBinnedLocation(new LatLon(41, -51.000001));

...

private Location getBinnedLocation(Location loc)
{
    return new Location(
        getBinnedCoord(loc.Latitude, true),
        getBinnedCoord(loc.Longitude, false));
}
我对来自Java的C#比较陌生,但我的理解是继承等原则基本上保持不变。那么为什么行
latlonl1=(LatLon)getBinnedLocation(newlatlon(41,-51.000001))
导致错误:
与“SomeClass.getBinnedLocation(Bing.Maps.Location)”匹配的最佳重载方法具有一些无效参数

这似乎是一个非常简单的问题,有一个非常简单的解决方案,但我一直无法找出问题所在

编辑:

证明我的位置引用的是Bing.Maps的位置,而不是我命名空间中的另一个位置类:

错误的图片:

错误3“对象”不包含接受2个参数的构造函数C:\…\LatLon.cs 16 15 MapTest 错误4当前上下文C:\…\LatLon.cs 36 21 MapTest中不存在名称“Latitude” 错误5“MapTest.LatLon”不包含“Latitude”的定义,并且找不到接受“MapTest.LatLon”类型的第一个参数的扩展方法“Latitude”(是否缺少using指令或程序集引用?)C:\…\LatLon.cs 36 35 MapTest 错误6当前上下文C:\…\LatLon.cs 36 49 MapTest中不存在名称“经度” 错误7“MapTest.LatLon”不包含“经度”的定义,并且找不到接受“MapTest.LatLon”类型的第一个参数的扩展方法“经度”(是否缺少using指令或程序集引用?)C:\…\LatLon.cs 36 64 MapTest 错误8当前上下文C:\…\LatLon.cs 48 21 MapTest中不存在名称“Latitude” 错误9“MapTest.LatLon”不包含“Latitude”的定义,并且找不到接受“MapTest.LatLon”类型的第一个参数的扩展方法“Latitude”(是否缺少using指令或程序集引用?)C:\…\LatLon.cs 48 35 MapTest 错误10当前上下文C:\…\LatLon.cs 48 49 MapTest中不存在名称“经度” 错误11“MapTest.LatLon”不包含“Longitude”的定义,并且找不到接受“MapTest.LatLon”类型的第一个参数的扩展方法“Longitude”(是否缺少using指令或程序集引用?)C:\…\LatLon.cs 48 64 MapTest 错误12当前上下文C:\…\LatLon.cs 54 37 MapTest中不存在名称“Latitude” 错误13当前上下文C:\…\LatLon.cs 55 37 MapTest中不存在名称“经度” 错误14“MapTest.MainPage.getBinnedLocation(Bing.Maps.Location)”的最佳重载方法匹配具有一些无效参数C:\…\MainPage.xaml.cs 75 25 MapTest 错误15参数1:无法从“MapTest.LatLon”转换为“Bing.Maps.Location”C:\…\MainPage.xaml.cs 75 43 MapTest 错误16“MapTest.MainPage.getBinnedLocation(Bing.Maps.Location)”的最佳重载方法匹配具有一些无效参数C:\…\MainPage.xaml.cs 76 25 MapTest 错误17参数1:无法从“MapTest.LatLon”转换为“Bing.Maps.Location”C:\…\MainPage.xaml.cs 76 43 MapTest 错误18“MapTest.LatLon”不包含“Latitude”的定义,并且找不到接受“MapTest.LatLon”类型的第一个参数的扩展方法“Latitude”(是否缺少using指令或程序集引用?)C:\…\MainPage.xaml.cs 79 40 MapTest 错误19“MapTest.LatLon”不包含“Longitude”的定义,并且找不到接受类型为“MapTest.LatLon”的第一个参数的扩展方法“Longitude”(是否缺少using指令或程序集引用?)C:\…\MainPage.xaml.cs 79 61 MapTest 错误20“MapTest.LatLon”不包含“Latitude”的定义,并且找不到接受“MapTest.LatLon”类型的第一个参数的扩展方法“Latitude”(是否缺少using指令或程序集引用?)C:\…\MainPage.xaml.cs 80 40 MapTest 错误21“MapTest.LatLon”不包含“Longitude”的定义,并且找不到接受类型为“MapTest.LatLon”的第一个参数的扩展方法“Longitude”(是否缺少using指令或程序集引用?)C:\…\MainPage.xaml.cs 80 61 MapTest
将LatLon实例发送到接收某个位置的函数时,将其强制转换到某个位置。

LatLon类存在一些问题,可能是这些问题的原因。查看修复这些问题是否修复了代码

  • 您可以创建自己的Latitude和Logitude成员。您不应该创建自己的,您应该使用基础类的成员,您可以通过
    base(
    )调用基础类的构造函数
  • getBinnedLocation
    返回一个
    新位置
    ,但您将其强制转换为
    LatLon
    ,这将在运行时失败。只需调用
    new LatLon
    ,让它返回一个
    LatLon
    ,而不是
    位置
  • 您的函数使用java的命名风格C#has(不是错误,但您应该遵循您正在编写的语言的约定)。函数应该是
    PascalCased
    Not
    camelCased
  • 更新:

    感谢您更新的问题,您出现问题(错误15)的原因确实是错误1-14阻止了类的编译。修复1-14将修复您的错误

    从您的描述来看,似乎VS对类型感到困惑。它的作用好像位置不是定义的类型。Tr Error 3 'object' does not contain a constructor that takes 2 arguments C:\...\LatLon.cs 16 15 MapTest Error 4 The name 'Latitude' does not exist in the current context C:\...\LatLon.cs 36 21 MapTest Error 5 'MapTest.LatLon' does not contain a definition for 'Latitude' and no extension method 'Latitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\LatLon.cs 36 35 MapTest Error 6 The name 'Longitude' does not exist in the current context C:\...\LatLon.cs 36 49 MapTest Error 7 'MapTest.LatLon' does not contain a definition for 'Longitude' and no extension method 'Longitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\LatLon.cs 36 64 MapTest Error 8 The name 'Latitude' does not exist in the current context C:\...\LatLon.cs 48 21 MapTest Error 9 'MapTest.LatLon' does not contain a definition for 'Latitude' and no extension method 'Latitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\LatLon.cs 48 35 MapTest Error 10 The name 'Longitude' does not exist in the current context C:\...\LatLon.cs 48 49 MapTest Error 11 'MapTest.LatLon' does not contain a definition for 'Longitude' and no extension method 'Longitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\LatLon.cs 48 64 MapTest Error 12 The name 'Latitude' does not exist in the current context C:\...\LatLon.cs 54 37 MapTest Error 13 The name 'Longitude' does not exist in the current context C:\...\LatLon.cs 55 37 MapTest Error 14 The best overloaded method match for 'MapTest.MainPage.getBinnedLocation(Bing.Maps.Location)' has some invalid arguments C:\...\MainPage.xaml.cs 75 25 MapTest Error 15 Argument 1: cannot convert from 'MapTest.LatLon' to 'Bing.Maps.Location' C:\...\MainPage.xaml.cs 75 43 MapTest Error 16 The best overloaded method match for 'MapTest.MainPage.getBinnedLocation(Bing.Maps.Location)' has some invalid arguments C:\...\MainPage.xaml.cs 76 25 MapTest Error 17 Argument 1: cannot convert from 'MapTest.LatLon' to 'Bing.Maps.Location' C:\...\MainPage.xaml.cs 76 43 MapTest Error 18 'MapTest.LatLon' does not contain a definition for 'Latitude' and no extension method 'Latitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\MainPage.xaml.cs 79 40 MapTest Error 19 'MapTest.LatLon' does not contain a definition for 'Longitude' and no extension method 'Longitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\MainPage.xaml.cs 79 61 MapTest Error 20 'MapTest.LatLon' does not contain a definition for 'Latitude' and no extension method 'Latitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\MainPage.xaml.cs 80 40 MapTest Error 21 'MapTest.LatLon' does not contain a definition for 'Longitude' and no extension method 'Longitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\MainPage.xaml.cs 80 61 MapTest
    class LatLon : Location
    {
        //Get rid of these
        //public readonly double Latitude;
        //public readonly double Longitude;
    
        public LatLon(double latitude, double longitude)
              : base(latitude, longitude) //This calls the base's (double, double) constuctor.
        {
        }
    
        //... Everything else can stay the same
    }
    
    
    
    LatLon l1 = GetBinnedLocation(new LatLon(41, -51.000001));
    
    ...
    
    private LatLon GetBinnedLocation(Location loc)
    {
        return new LatLon(
            getBinnedCoord(loc.Latitude, true),
            getBinnedCoord(loc.Longitude, false));
    }