如何使用两个参数实现Default Item属性 [英] how to implement a Default Item property with two Parameters

查看:94
本文介绍了如何使用两个参数实现Default Item属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个篮球队的球员集合,其中包含一个哈希表,用于通过他们的JerseyNumber查找球员。



但是我遇到的问题是我不能简单地重载我的Item属性,它根据索引值返回一个播放器,因为重载方法除了整数类型之外也是如此。



我需要知道的是可以在默认的Item属性中包含两个参数:



1.对于索引参数

2.对于jerseynumber



并让属性确定提供了哪个参数并使用适当的方法返回一个玩家? div class =h2_lin>解决方案

形式上,这是索引属性。具体如下:

  class  PlayerSet {

// ...

internal 播放器 [ int 索引, int jerseyNumber] {
get {
return FindPlayer(index,jerseyNumber); // 但这取决于您返回的内容;它可能是矛盾的
} // get Default
} // 默认

} // class PlayerSet





然而,如果这是值得怀疑的整个想法是有道理的。通常,您可以按索引或零项找到一个单项,按键或零项找到一个单项。关键和索引可能会相互矛盾。也许你应该更好地使用两个不同的属性,这是许多问题的原因,因为很多人都没有弄清楚语法。其中一个问题是索引和密钥的相同类型。这是其中一种方式:

  class  JerseyNumber { //  只是包装器,创建不同的类型;你可以想到不同的东西 
内部 JerseyNumber( int index){< span class =code-keyword> this .Index = index; }
internal int 索引{ get ; private set ; }
} // class JerseyNumber

interface IKeyedPlayerSet {
Player this [JerseyNumber jerseyNumber] { get < /跨度>; }
} // interface IKeyedPlayerSet

class PlayerSet:IKeyedPlayerSet {

public 播放器 this [JerseyNumber jerseyNumber] {
return FindPlayerByKey(jerseyNumber.Index); // 按整数键
} // IKeyedPlayerSet.this

内部播放器 this [ int index] { / * 按索引访问* / }

// 。 ..

} // class PlayerSet

这些只是想法。我不知道你的播放器收藏实现的一些重要细节,所以我的样本中的某些东西可能不适合它,但我希望你仍然可以弄清楚该怎么做。



-SA


不,两个名称相同的方法(在本例中为Item)不能具有相同的原型。编译器无法解决歧义(这几乎是编译器错误所说的,模棱两可的方法)。



为此我将完全消除歧义并创建一种派生自Hashtable的类型。添加两个方法,GetByIndex和GetByJersey。想想以后读这个软件的人,他们怎么知道32是球衣号码而不是索引?


Hi,

I have a collection of players on a basketball team which contains a hashtable within the collection to lookup players by their JerseyNumber.

However the issue I have is that I cannot simply overload my Item property that returns a player based on an index value as the overloaded method would except a integer type as well.

What I need to know is it possible to include two parameters in the default Item property:

1. For index parameter
2. For the jerseynumber

and have the property determine which argument was supplied and use the appropriate method to return a player?

解决方案

Formally, this is the indexed property. This is how:

class PlayerSet {
 
   //...

   internal Player this[int index, int jerseyNumber] {
      get {
         return FindPlayer(index, jerseyNumber); // but it's up to you what you return; it could be contradictory
      } //get Default
   } //Default

} //class PlayerSet



However, it's questionable if the whole idea makes sense. Usually, you can find one single item by index, or zero items, and one single item by key, or zero items. The key and index may contradict. Perhaps you should better use two different property, any this was the reason of many questions, because many people failed to figure out the syntax. One of the problems is identical types of the index and the key. This is one of the ways:

class JerseyNumber { // just the wrapper, to create different type; you can think of something different
   internal JerseyNumber(int index) { this.Index = index; }
   internal int Index { get; private set; }
} //class JerseyNumber

interface IKeyedPlayerSet {
    Player this[JerseyNumber jerseyNumber] { get; }
} //interface IKeyedPlayerSet

class PlayerSet : IKeyedPlayerSet {

    public Player this[JerseyNumber jerseyNumber] {
       return FindPlayerByKey(jerseyNumber.Index); // by integer key
    } //IKeyedPlayerSet.this

    internal Player this[int index] { /* access by index */ }

    //...
   
} //class PlayerSet

These are just the ideas. I don't know some important detail of your implementation of player collection, so something in my samples may be not adequate to it, but I hope you still can figure out what to do.

—SA


No, two identically named methods (in this case Item) can't have the same prototypes. The compiler would not be able to resolve the ambiguity (which is almost what the compiler error says, ambiguous method).

For this I would remove the ambiguity entirely and create a type that derives from Hashtable. Add two methods, GetByIndex and GetByJersey. Think about the person reading the software later, how are they supposed to know that 32 is the jersey number and not the index?


这篇关于如何使用两个参数实现Default Item属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆