假设我有一个包含许多实例变量的类.我想重载==运算符(和hashCode),以便我可以将实例用作映射中的键.
class Foo {
int a;
int b;
SomeClass c;
SomeOtherClass d;
// etc.
bool operator==(Foo other) {
// Long calculation involving a, b, c, d etc.
}
}
Run Code Online (Sandbox Code Playgroud)
比较计算可能很昂贵,所以我想检查是否other与this进行计算前的实例相同.
如何调用Object类提供的==运算符来执行此操作?
我只是在阅读Dart语言规范并探索一种有趣的新语言。正如Dart语言规范所说:Dart具有implicit interfaces。这意味着每个class都是interface。因此,如果我想实现另一个类的某些行为,implements则仅需要子句。
此外,Dart支持mixins。这样我们就可以使用with关键字从另一个类中实现方法。
因此,假设抽象类A定义了方法a():
abstract class A {
void a();
}
Run Code Online (Sandbox Code Playgroud)
另外两个具体的类B定义方法a(),但不实现类A,例如:
class B {
void a() {
print("I am class B");
}
}
Run Code Online (Sandbox Code Playgroud)
C类使用Mixin B实现A类,例如:
class C extends Object with B implements A {
...
}
Run Code Online (Sandbox Code Playgroud)
在这里,我对此几乎没有疑问。如果一个类实现了该接口并且还使用了具有相同方法名的方法实现的mixin;是不是有cycling inheritance可能?的行为是class C什么?是需要实现a()还是将隐式实现mixin B?
我只是在学习Dart,而像mixins这样的概念对我来说却并不熟悉。有人可以回答我的问题来帮助我理解吗?
我创建了一个闭包的文字地图,例如:
Map<String, Function> mapOfFuncs = {
'foo': (a, b, c) => ... ,
'bar': (a, b, c) => ... ,
...
}
Run Code Online (Sandbox Code Playgroud)
到目前为止都很好.然后我想制作这张地图const,因为它在我的程序中是全局的,不应该被修改.
const Map<String, Function> MAP_OF_FUNCS = const {
'foo': (a, b, c) => ... ,
'bar': (a, b, c) => ... ,
...
}
Run Code Online (Sandbox Code Playgroud)
由于地图中的字面闭包不是,所以Dart扼杀了这一点const.
在Dartpad:https://dartpad.dartlang.org/817d2cfd141b0a56fc7d
我原本认为文字封闭是const.有没有办法让它们如此?
我正在使用 dart 开发一个应用程序,我想获取被点击元素的值。IE:
网址:
<div id="blah">Value!!</div>
Run Code Online (Sandbox Code Playgroud)
镖:
void main(){
query("#blah").onClick.listen(showValue);
}
void showValue(Event e){
//Get #blah's innerHtml based on the Event(Something like e.target.innerHtml or e.target.value)
}
Run Code Online (Sandbox Code Playgroud)
任何人?我知道如何在 JS 中做到这一点,但我想知道是否有办法在 DART 中做到这一点!
编辑
谢谢大家,我会尽量说得更清楚一点。我有一个动态生成 9 个元素并将它们添加到容器的循环。
代码:
var j = 0;
var optionsSquare = query("#optionsPanel");
while(j < 9){
DivElement minorSquare = new DivElement();
var minorSquareHeight = optionsSquare.clientHeight/3;
var minorSquareWidth = optionsSquare.clientWidth/3;
minorSquare
..attributes = {"class": "miniSugg"}
..style.width = (minorSquareWidth.round()-2).toString().concat("px")
..style.height = (minorSquareHeight.round()-2).toString().concat("px")
..style.float = "left"
..style.border = "1px solid"
..innerHtml = …Run Code Online (Sandbox Code Playgroud)