简体   繁体   English

在主 function - dart 中调用 class 方法

[英]call class method in main function - dart

I am beginner in dart and I am currently learning about classes and methods.我是 dart 的初学者,目前正在学习类和方法。 I just want call the method I have created in the interest class to the main function so that it performs the define operations.我只想将我在兴趣 class 中创建的方法调用到主 function 以便它执行定义操作。 I don't want to implement any functions or variables in main.我不想在 main.js 中实现任何函数或变量。 I don't know if it is possible I am trying out something;我不知道我是否有可能尝试一些东西; that probably when you want hide some business logic from the public.这可能是当您想向公众隐藏一些业务逻辑时。 please help me.请帮我。 I believe there is but I just can't find it.我相信有,但我就是找不到。

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) {

}

make your function static, like this:让你的 function static,像这样:

static double interestAmnt()

And in your main, call your function like this:在你的主要,像这样调用你的 function :

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

There are many ways to do what you like.有很多方法可以做你喜欢的事情。 Dart support top level function but since you prefer creating an object that holds its own business logic, I modified your example to work with some comments: Dart 支持顶级 function 但由于您更喜欢创建拥有自己的业务逻辑的 object,因此我修改了您的示例以使用一些注释:

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));
  }
}

The main problem in your example you were defining functions in the constructor body that were not accessible by the class itself.您的示例中的主要问题是在构造函数主体中定义 class 本身无法访问的函数。 A lot of these terms may sound new to you, but overtime you'll get used to it.许多这些术语对您来说可能听起来很新,但加班后您会习惯的。

Here's a link for the language tour where you can learn more about Dart: https://dart.dev/guides/language/language-tour这是语言之旅的链接,您可以在其中了解有关 Dart 的更多信息: https://dart.dev/guides/language/language-tour

I was able to solve the issue with the guidance above but I still believe there is another way out.我能够通过上述指导解决问题,但我仍然相信还有另一种出路。 thanks a lot guys;非常感谢你们; Thanks very much!非常感谢!

// 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();
 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM