C# 将派生类作为参数传递给用于基类参数的方法-获取错误
在有人指出之前,我已经读过了,运气不好 我有一个LatLon类,它扩展了Bing.Maps的Location类(),主要是重写GetHashCode和Equals方法来执行值相等,而不是内置的引用相等,如下所示: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
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类存在一些问题,可能是这些问题的原因。查看修复这些问题是否修复了代码
base(
)调用基础类的构造函数getBinnedLocation
返回一个新位置
,但您将其强制转换为LatLon
,这将在运行时失败。只需调用new LatLon
,让它返回一个LatLon
,而不是位置
PascalCased
NotcamelCased
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));
}