Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 如何正确实现将对象作为关键点的贴图?_Dart - Fatal编程技术网

Dart 如何正确实现将对象作为关键点的贴图?

Dart 如何正确实现将对象作为关键点的贴图?,dart,Dart,我试图实现这个逻辑:我有一副牌,上面有几张牌。每张卡都有一套西装和一个价值。但是,牌组中可能有重复的牌。我想数一数每副牌中有多少张。西服是一个枚举,还有一张卡类: enum Suit { Red, Green, Blue }; class Card { Suit suit; int value; Card(this.suit, this.value); } 这将是甲板: final deck = Map<Card, int>(); final addCardToD

我试图实现这个逻辑:我有一副牌,上面有几张牌。每张卡都有一套西装和一个价值。但是,牌组中可能有重复的牌。我想数一数每副牌中有多少张。西服是一个枚举,还有一张
类:

enum Suit { Red, Green, Blue };

class Card {
  Suit suit;
  int value;

  Card(this.suit, this.value);
}
这将是甲板:

final deck = Map<Card, int>();

final addCardToDeck = (Card c) {
  if (deck[c] != null) deck[c]++;
  else deck[c] = 1;
};
由于这两张卡是相等的,我希望
deck[cardA]
deck[cardB]
返回2,对吗?错了!两人都返回1。所以我想,好吧,一定是对象引用问题,我将重载
=
操作符

bool operator ==(otherCard) {
    return otherCard is Card 
      && suit == otherCard.suit 
      && value == otherCard.value;
}
但它仍然不能像预期的那样工作。那么,我该如何正确地实现这一点呢?我知道我可以制作一张地图,所以我会像
deck[suit][value]
那样访问它,但我发现这种方法更整洁。这是否可行

下面是完整的代码

enum Suit { Red, Blue, Green }

class Card {
  Suit suit;
  int value;

  Card(this.suit, this.value);

  bool operator ==(otherCard) {
    return otherCard is Card && suit == otherCard.suit && value == otherCard.value;
  }
}

void main() {
  final deck = Map<Card, int>();

  final addCardToDeck = (Card c) {
    if (deck[c] != null) deck[c]++;
    else deck[c] = 1;
  };

  final cardA = Card(Suit.Red, 7);
  final cardB = Card(Suit.Red, 7);

  addCardToDeck(cardA);
  addCardToDeck(cardB);

  print(deck[cardA]); // Expected 2, got 1
  print(deck[cardB]); // Expected 2, got 1
}
enum套装{红、蓝、绿}
班级卡{
西服;
int值;
卡片(这套衣服,这张价值);
布尔运算符==(其他卡){
返回otherCard is Card&&suit==otherCard.suit&&value==otherCard.value;
}
}
void main(){
最终甲板=地图();
最终addCardToDeck=(卡c){
如果(deck[c]!=null)deck[c]++;
else甲板[c]=1;
};
最终卡片=卡片(红色套装,7);
最终卡片B=卡片(红色,7);
addCardToDeck(cardA);
addCardToDeck(cardB);
打印(卡片组[cardA]);//预期为2,实际为1
打印(卡片组[cardB]);//预期为2,实际为1
}

hashCode
将用于确定
映射中的相等性。如果覆盖
==
,则还应覆盖
hashCode

以下是更新的代码:

enum Suit { Red, Blue, Green }

class Card {
  Suit suit;
  int value;

  Card(this.suit, this.value);

  @override
  bool operator ==(otherCard) {
    return otherCard is Card &&
        suit == otherCard.suit &&
        value == otherCard.value;
  }

  @override
  int get hashCode => suit.hashCode^value.hashCode;
}

void main() {
  final deck = <Card, int>{};

  final addCardToDeck = (Card c) {
    if (deck[c] != null) {
      deck[c]++;
    } else {
      deck[c] = 1;
    }
  };

  final cardA = Card(Suit.Red, 7);
  final cardB = Card(Suit.Red, 7);
  final cardC = Card(Suit.Green, 4);

  addCardToDeck(cardA);
  addCardToDeck(cardB);
  addCardToDeck(cardC);

  print(deck[cardA]); // Expected 2, got 2
  print(deck[cardB]); // Expected 2, got 2
  print(deck[cardC]); // Expected 1, got 1
}
enum套装{红、蓝、绿}
班级卡{
西服;
int值;
卡片(这套衣服,这张价值);
@凌驾
布尔运算符==(其他卡){
返回其他卡是卡&&
suit==otherCard.suit&&
value==otherCard.value;
}
@凌驾
int get hashCode=>suit.hashCode^value.hashCode;
}
void main(){
最终甲板={};
最终addCardToDeck=(卡c){
如果(组[c]!=null){
甲板[c]++;
}否则{
甲板[c]=1;
}
};
最终卡片=卡片(红色套装,7);
最终卡片B=卡片(红色,7);
最终卡片C=卡片(套装绿色,4);
addCardToDeck(cardA);
addCardToDeck(cardB);
addCardToDeck(cardC);
打印(卡片组[cardA]);//预期为2,实际为2
打印(卡片组[cardB]);//预期为2,实际为2
打印(卡片组[cardC]);//预期为1,实际为1
}

hashCode
将用于确定
映射中的相等性。如果覆盖
==
,则还应覆盖
hashCode

以下是更新的代码:

enum Suit { Red, Blue, Green }

class Card {
  Suit suit;
  int value;

  Card(this.suit, this.value);

  @override
  bool operator ==(otherCard) {
    return otherCard is Card &&
        suit == otherCard.suit &&
        value == otherCard.value;
  }

  @override
  int get hashCode => suit.hashCode^value.hashCode;
}

void main() {
  final deck = <Card, int>{};

  final addCardToDeck = (Card c) {
    if (deck[c] != null) {
      deck[c]++;
    } else {
      deck[c] = 1;
    }
  };

  final cardA = Card(Suit.Red, 7);
  final cardB = Card(Suit.Red, 7);
  final cardC = Card(Suit.Green, 4);

  addCardToDeck(cardA);
  addCardToDeck(cardB);
  addCardToDeck(cardC);

  print(deck[cardA]); // Expected 2, got 2
  print(deck[cardB]); // Expected 2, got 2
  print(deck[cardC]); // Expected 1, got 1
}
enum套装{红、蓝、绿}
班级卡{
西服;
int值;
卡片(这套衣服,这张价值);
@凌驾
布尔运算符==(其他卡){
返回其他卡是卡&&
suit==otherCard.suit&&
value==otherCard.value;
}
@凌驾
int get hashCode=>suit.hashCode^value.hashCode;
}
void main(){
最终甲板={};
最终addCardToDeck=(卡c){
如果(组[c]!=null){
甲板[c]++;
}否则{
甲板[c]=1;
}
};
最终卡片=卡片(红色套装,7);
最终卡片B=卡片(红色,7);
最终卡片C=卡片(套装绿色,4);
addCardToDeck(cardA);
addCardToDeck(cardB);
addCardToDeck(cardC);
打印(卡片组[cardA]);//预期为2,实际为2
打印(卡片组[cardB]);//预期为2,实际为2
打印(卡片组[cardC]);//预期为1,实际为1
}

Heads up我更正了解决方案中的错误(哈希代码计算错误)Heads up我更正了解决方案中的错误(哈希代码计算错误)