[英]call class method in main function - dart
我是 dart 的初学者,目前正在学习类和方法。 我只想将我在兴趣 class 中创建的方法调用到主 function 以便它执行定义操作。 我不想在 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 static,像这样:
static double interestAmnt()
在你的主要,像这样调用你的 function :
main(List<String> args) {
Interest.interestAmnt();
}
有很多方法可以做你喜欢的事情。 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));
}
}
您的示例中的主要问题是在构造函数主体中定义 class 本身无法访问的函数。 许多这些术语对您来说可能听起来很新,但加班后您会习惯的。
这是语言之旅的链接,您可以在其中了解有关 Dart 的更多信息: https://dart.dev/guides/language/language-tour
我能够通过上述指导解决问题,但我仍然相信还有另一种出路。 非常感谢你们; 非常感谢!
// 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.