Android super.function()的位置是否会导致代码中出现执行问题?
例如,我想知道在flatter/dart中放置Android super.function()的位置是否会导致代码中出现执行问题?,android,flutter,dart,Android,Flutter,Dart,例如,我想知道在flatter/dart中放置super.function()是否会影响其余代码的执行。创建和配置控制器 因为。例如 @override void foo() { super.function(); controller.function(); } 如果在controller.function()之后有super.function(),或者在那些大括号中有任何代码,代码会出现错误吗?或者我应该把它放在controller.function()前面 注
super.function()
是否会影响其余代码的执行。创建和配置控制器
因为。例如
@override
void foo() {
super.function();
controller.function();
}
如果在controller.function()
之后有super.function()
,或者在那些大括号中有任何代码,代码会出现错误吗?或者我应该把它放在controller.function()前面
注意:我已经概括了代码
问题并不像你想象的那么简单,答案取决于你想要实现什么 如果要在超级实现执行之前覆盖某些内容,则在覆盖要覆盖的内容之后调用超级。例如:
class DoTheMathAndPrint {
int first = 1;
int second = 2;
void iWillDoTheMathAndPrint() {
int sum = first + second;
print("The sum is $sum");
}
}
class NoIWillDoTheMath extends DoTheMathAndPrint {
@override
void iWillDoTheMathAndPrint() {
first = 3;
second = 5;
super.iWillDoTheMathAndPrint();
// will print The sum is 8;
}
}
class WellIHateMath extends DoTheMathAndPrint {
@override
void iWillDoTheMathAndPrint() {
super.iWillDoTheMathAndPrint();
print("I hope it printed $3");
}
}
class SomeAwesomeState<T extends StatefulWidget> extends State<T> {
List<SomeBigObject> _theList = ...smething big;
@override
void dispose() {
_theList.clear();
super.dispose();
}
}
class MyStateWithAnimation extends SomeAwesomeState<AnimationWidget> with SingleTickerProviderStateMixin {
// some awesome animations here
@override
void dispose() {
//remove whatever here
super.dispose(); => this will be the super on the SingleTickerProviderStateMixin not on the SomeAwesomeState
}
}
如果您想要正常的流,然后“扩展”流,您将首先调用super,然后执行代码。例如:
class DoTheMathAndPrint {
int first = 1;
int second = 2;
void iWillDoTheMathAndPrint() {
int sum = first + second;
print("The sum is $sum");
}
}
class NoIWillDoTheMath extends DoTheMathAndPrint {
@override
void iWillDoTheMathAndPrint() {
first = 3;
second = 5;
super.iWillDoTheMathAndPrint();
// will print The sum is 8;
}
}
class WellIHateMath extends DoTheMathAndPrint {
@override
void iWillDoTheMathAndPrint() {
super.iWillDoTheMathAndPrint();
print("I hope it printed $3");
}
}
class SomeAwesomeState<T extends StatefulWidget> extends State<T> {
List<SomeBigObject> _theList = ...smething big;
@override
void dispose() {
_theList.clear();
super.dispose();
}
}
class MyStateWithAnimation extends SomeAwesomeState<AnimationWidget> with SingleTickerProviderStateMixin {
// some awesome animations here
@override
void dispose() {
//remove whatever here
super.dispose(); => this will be the super on the SingleTickerProviderStateMixin not on the SomeAwesomeState
}
}
现在,当我处理框架的子类(flatter、Android、iOS)时,我通常做的是决定一个方法是“建设性的”还是“破坏性的”。“构造性”方法是一些初始化代码片段、配置等的方法(如颤振中的initState
)。对于这种我首先称之为super的方法,我希望确保一切都是由框架设置的,然后我就可以做我的事情了
对于“破坏性”方法,即清理资源、释放内存等的方法(如flatter中的dispose()
),我称之为super last,我释放了我的资源,然后我可以对框架说:“一切都好,做你的工作”
dart中有一件事让我震惊(因为我读得不够多),那就是使用mixin时的超级调用。您必须非常小心,超级类上不会调用super
,它将在mixin上调用。例如:
class DoTheMathAndPrint {
int first = 1;
int second = 2;
void iWillDoTheMathAndPrint() {
int sum = first + second;
print("The sum is $sum");
}
}
class NoIWillDoTheMath extends DoTheMathAndPrint {
@override
void iWillDoTheMathAndPrint() {
first = 3;
second = 5;
super.iWillDoTheMathAndPrint();
// will print The sum is 8;
}
}
class WellIHateMath extends DoTheMathAndPrint {
@override
void iWillDoTheMathAndPrint() {
super.iWillDoTheMathAndPrint();
print("I hope it printed $3");
}
}
class SomeAwesomeState<T extends StatefulWidget> extends State<T> {
List<SomeBigObject> _theList = ...smething big;
@override
void dispose() {
_theList.clear();
super.dispose();
}
}
class MyStateWithAnimation extends SomeAwesomeState<AnimationWidget> with SingleTickerProviderStateMixin {
// some awesome animations here
@override
void dispose() {
//remove whatever here
super.dispose(); => this will be the super on the SingleTickerProviderStateMixin not on the SomeAwesomeState
}
}
类SomeAwesomeState扩展状态{
列表_theList=…非常大;
@凌驾
无效处置(){
_清除列表();
super.dispose();
}
}
类MyStateWithAnimation使用SingleTickerProviderStateMixin扩展SomeAsomeState{
//这里有一些很棒的动画
@凌驾
无效处置(){
//把这里的东西拿走
super.dispose();=>这将是SingleTickerProviderStateMixin上的super,而不是SomeAwesomeState上的super
}
}
我希望这能在某种程度上回答您的问题,因为这是一个类似的想法。谢谢@dylan myers的评论!是的,它们很相似。然而,我有一个专门针对dart/FLUTER的问题,因为它们的工作方式可能不同于Java。例如,dart具有设计内置的异步性。如果dart/Flatter本身在基础中的工作与java等其他语言类似,那么是的,我的问题得到了回答。