【问题标题】:call class method in main function - dart在主函数中调用类方法 - 飞镖
【发布时间】:2021-02-24 20:54:29
【问题描述】:

我是 dart 的初学者,目前正在学习类和方法。我只想将我在兴趣类中创建的方法调用到主函数,以便它执行定义操作。我不想在 main.js 中实现任何函数或变量。我不知道我是否有可能尝试一些东西;这可能是当您想向公众隐藏一些业务逻辑时。请帮我。我相信有,但我就是找不到。

import 'dart:io';

class Interest {
double principal;
double rate;
int time;

Interest(double aPrincipal, double aRate, int aTime) {
 this.principal = aPrincipal;
 this.rate = aRate;
 this.time = aTime;
}

double interestAmnt() {
 print(
     "This is a program to Calculate the over all amount back for your loan");
 print("What is the Amount of loan you have taken: ");
 this.principal = double.parse(stdin.readLineSync());
 print("What is the interest on your loan: ");
 this.rate = double.parse(stdin.readLineSync());
 print("How long are paying for the long: ");
 this.time = int.parse(stdin.readLineSync());

 return this.principal * (1 + (this.rate * this.time));
}
}

main(List<String> args) {

}

【问题讨论】:

    标签: function flutter class dart methods


    【解决方案1】:

    让你的函数静态化,像这样:

    static double interestAmnt()
    

    在你的 main 中,像这样调用你的函数:

    main(List<String> args) {
      Interest.interestAmnt();
    }
    

    【讨论】:

    • 感谢按预期工作,但构造函数变量有下划线; “this”,所以我必须清除新变量,但我想要的是让类对象直接的函数。
    【解决方案2】:

    有很多方法可以做你喜欢做的事。 Dart 支持顶级功能,但由于您更喜欢创建一个拥有自己业务逻辑的对象,因此我修改了您的示例以使用一些 cmets:

    import 'dart:io';
    
    void main(List<String> args) {
      // you should get your variables here, since it's not the responsibility of the Interest class 
      print('This is a program to Calculate the over all amount back for your loan');
      print('What is the Amount of loan you have taken: ');
      final principal = double.parse(stdin.readLineSync());
      print('What is the interest on your loan: ');
      final rate = double.parse(stdin.readLineSync());
      print('How long are paying for the long: ');
      final time = int.parse(stdin.readLineSync());
    
      // Now you've all the variables needed to create an interest object, 
      // so create the object which has all the business logic
      final interest = Interest(principal, rate, time);
    
      // do operations on the created object
      print(interest.interestAmnt());
    }
    
    // this is an interest object with one constructor, three class members, and one method.
    class Interest {
      // mark these as final since they'll not change in the lifetime of the object
      final double principal;
      final double rate;
      final int time;
    
      // unlike some other languages, you can do it this way in dart:
      Interest(this.principal, this.rate, this.time);
      // constructor body isn't necessary in this case since we are taking the variables as is
      // take a look at the dart language tour to understand:
      // - how constructors work,
      // - initializer list
      // - constructor body
      // each has its own use case but for your case, this works fine
    
      // this is a class method that can has access to the instance variables
      // previously, you had this placed inside the constructor body and so it 
      // so it was inaccessible 
      double interestAmnt() {
        return principal * (1 + (rate * time));
      }
    }
    
    

    您的示例中的主要问题是您在构造函数主体中定义了类本身无法访问的函数。这些术语中的许多术语对您来说可能听起来很陌生,但随着时间的推移您会习惯的。

    这里是语言导览的链接,您可以在其中了解有关 Dart 的更多信息: https://dart.dev/guides/language/language-tour

    【讨论】:

      【解决方案3】:

      我能够通过上述指导解决问题,但我仍然相信还有其他出路。非常感谢你们;非常感谢!

      // interest rate
      
      import 'dart:io';
      
      class Interest {
        static double principal;
        static double rate;
        static double time;
      
        
      
        static double interestAmnt() {
          print(
              "This is a program to Calculate the over all amount back for your loan");
          print("What is the Amount of loan you have taken: ");
          principal = double.parse(stdin.readLineSync());
          print("What is the interest on your loan: ");
          rate = double.parse(stdin.readLineSync());
          print("How long are paying for the long: ");
          time = double.parse(stdin.readLineSync());
          double res = principal * (1 + (rate * time));
          print(res);
      
          return res;
        }
      }
      
      main(List<String> args) {
        Interest.interestAmnt();
       
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-31
        • 2021-12-05
        • 2020-11-28
        相关资源
        最近更新 更多