如何调用导入到另一个类的另一个方法?

时间:2019-05-14 07:41:06

标签: oop dart flutter

我想调用从three类方法导入到two类文件的one类方法。

我尝试过,但这是错误的。

文件名:one.dart

import 'two.dart';

class one{
  main(){
    return two().three().add();
  }
}

文件名:two.dart

import 'three.dart';

class two extends one{
  static three   = new three();
}

文件名:three.dart

class three extends two{
  void add(int a, int b){

  }
}

我想从类add中调用one方法。怎么做?请帮忙吗?

1 个答案:

答案 0 :(得分:0)

  1. three是静态的。 two().three() => two.three()
  2. three是一个属性。 two.three() => two.three
  3. add方法的
  4. 个缺少的参数。 add() => add(1, 2) //仅以数字为例
  5. three属性名称和three类名称相同。 class three => class Three
  6. 缺少三个类型。 static three => static Three three

    class one{
      main(){
        return two.three.add(1, 2);
      }
    }
    
    class two extends one{
      static Three three = new Three();
    }
    
    class Three extends two{
         void add(int a, int b){
       }
    }
    

但是,对于良好的OOP,有依赖关系反转原则,即高级模块不应依赖于低级模块。 您的代码现在违反了这一原则。因此,我不建议您使用当前的代码。