父类中的功能需要检测对子类属性的更改 [英] Functionality in parent class needs to detect changes to child class properties

查看:123
本文介绍了父类中的功能需要检测对子类属性的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Persistent {
bool changed = false;

Persistent(){
print('这里应该添加一些东西来使这个工作。
}
}

class Child extends Persistent {
num number = 1;
//在这个类中不应该做这个工作。
}

main(){
Child item = new Child();
item.number = 2;
assert(item.changed == true); // failed
}

要求:目标是让儿童透明类。检测更改的功能不能存在于Child类中,只能存在于Persistent类中。



感谢Dart专家的帮助!我期待着您的回音。



这是正在进行的工作,以使这项工作:

  import'dart:io'; 
import'dart:async';
import'dart:convert';
import'package:observe / observe.dart';

类Persistent extends Object with ChangeNotifier {
bool changed = false;

Persistent(){
this.changes.listen((List< ChangeRecord> record)=> changed = false);
//this.changes.listen((List<ChangeRecord> record)=> changed = true); //相同的异常
}
}

类扩展子类{
@observable num number = 1;
//在这个类中不应该做这个工作。
}

main(){
Child item = new Child();
item.number = 2;
assert(item.changed == true);
}

上面给出了以下异常:

 未处理的异常:
'file:/// home / david / Dropbox / WebDevelopment / DODB / source / DODB / bin / dodb.dart':失败断言:第22行pos 10:'item.changed == true'不是true。
#0 main(file:///home/david/Dropbox/WebDevelopment/DODB/source/DODB/bin/dodb.dart:22:10)
#1 _startIsolate.isolateStartHandler(dart:isolate -patch / isolate_patch.dart:216)
#2 _RawReceivePortImpl._handleMessage(dart:isolate-patch / isolate_patch.dart:124)


解决方案

您可以使用 ChangeNotifier 类,如此问题的答案所示





另一个尝试是使用反射,但是这是不鼓励的,特别是在浏览器中。
上面的解决方案使用反射,但据我所知,当你运行 pub build 时,Smoke变换器会生成代替反射代码的代码。



编辑





只有在调用

 Observable.dirtyCheck();   import'package:observe / observe.dart'; 

类Persistent extends Observable {
bool changed = false;

Persistent():super(){
changes.listen((e)=> changed = true);
}
}

类Child扩展持久化{
@observable num number = 1;
}

main(){
Child item = new Child();
item.number = 2;
Observable.dirtyCheck();
assert(item.changed == true);
}


I am trying to find a way for this parent "Persistent" class to add functionality so that the "changed" property becomes true whenever any property of the child object is changed.

class Persistent {
  bool changed = false;

  Persistent() {
    print('Something should be added here to make this work.');
  }
}

class Child extends Persistent {
  num number = 1;
  // Nothing should be done in this class to make this work.
}

main() {
  Child item = new Child();
  item.number = 2;
  assert(item.changed == true); //fails
}

Requirement: The goal is for this to be transparent to the Child class. The functionality to detect changes must not exist in the Child class, only inside of the Persistent class.

Thank you Dart experts for your help! I look forward to hearing from you.

Here is the work in progress to get this working:

import 'dart:io';
import 'dart:async';
import 'dart:convert';
import 'package:observe/observe.dart';

class Persistent extends Object with ChangeNotifier {
  bool changed = false;

  Persistent() {
    this.changes.listen((List<ChangeRecord> record) => changed = false);
    //this.changes.listen((List<ChangeRecord> record) => changed = true); //Same exception
  }
}

class Child extends Persistent {
  @observable num number = 1;
  // Nothing should be done in this class to make this work.
}

main() {
  Child item = new Child();
  item.number = 2;
  assert(item.changed == true);
}

The above gives me the following exception:

Unhandled exception:
'file:///home/david/Dropbox/WebDevelopment/DODB/source/DODB/bin/dodb.dart': Failed assertion: line 22 pos 10: 'item.changed == true' is not true.
#0      main (file:///home/david/Dropbox/WebDevelopment/DODB/source/DODB/bin/dodb.dart:22:10)
#1      _startIsolate.isolateStartHandler (dart:isolate-patch/isolate_patch.dart:216)
#2      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:124)

解决方案

You can use the ChangeNotifier class like shown in the answers to this question

Another attempt is to use reflection but this is discouraged especially in the browser. The above solution uses reflection too but as far as I know the Smoke transformer generates code that replaces the reflective code when you run pub build.

edit

Only after a call to Observable.dirtyCheck(); a change detection is initiated (for all observable instances).

import 'package:observe/observe.dart';

class Persistent extends Observable {
  bool changed = false;

  Persistent() : super() {
    changes.listen((e) => changed = true);
  }
}

class Child extends Persistent {
  @observable num number = 1;
}

main() {
  Child item = new Child();
  item.number = 2;
  Observable.dirtyCheck();
  assert(item.changed == true);
}

这篇关于父类中的功能需要检测对子类属性的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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