Flutter / Dart:访问类方法以实现更清晰的代码实现访问。基于先前方法的构建步骤

发布于 2025-01-15 14:49:51 字数 2860 浏览 1 评论 0原文

我现在正在做这件事,现在已经是第三天了。我非常沮丧,因为我正在尝试编写更清晰的代码。

(第 1 部分):尝试将其变成一个类

// 获取方法的实例,但没有方法输出。

(第 2 部分):代码在 Dartpad 上运行

// 在类中需要此代码以及如何调用在其他类中使用的方法。

// (第 1 部分:类 FindPoundsToGallons 不起作用) // 尝试了几种不同的方法

// 我希望能够在整个应用程序中调用和重用这些类,这就是我想要的。 ..

// 当我在类中尝试它们时,我似乎无法调用正确的输出。

class FindPoundsToGallons {
  late num userInputSpgr;
  late num userInputLbsIssued;
  double numCon833 = 8.33;

  FindPoundsToGallons({
    required this.userInputSpgr,
    required this.userInputLbsIssued,
    required this.numCon833,
  });
}

void poundsToGallons() {
  num spgr = 1.5; // sample number
  num lbs = 15; // sample number
  num step1Result = findWeightOfFuel(spgr1: spgr);
  num step2Result = findGallons(step1Result, lbs1: lbs);
  num step3Result = findTotalWeight(step1Result, lbs1: lbs);
  num step4Result = finalResult(step1Result,step2Result,step3Result);
// ToDo: Tell user rounded down to the nearth tenth place

  // print(finalResult);
  print(step1Result.toStringAsFixed(2));
  print(step2Result.toStringAsFixed(1));
  print(step3Result.toStringAsFixed(1));
}

num findWeightOfFuel({required num spgr1}) {
  return (spgr1 * numCon833);
}

num findGallons(num step1result, {required num lbs1}) {
  return (lbs1 / step1result);
}

num findTotalWeight(num step2Result, {required num lbs1}) {
  return (step2Result * lbs1);
}
String finalResult(num step2Result, {required num lbs1}) {
  return ('$lbs / $spgi * numCon833 = $finalResult');
}

// 尝试的示例:

FindPoundsToGallons q1 = FindPoundsToGallons();

// 返回

q1.findWeightOfFuel //

磅换算加仑多步类方法

部分2: // 这部分适用于 Dartpad / 我在课堂上需要它。

// Formula: Pounds To Gallons
// Trying to put this into a class for later and trying to call on the methods but I keep getting instances of it without being able to see the output when I have them in a class I try to create. 


void main() {
  num spgr = 2;
  num lbs = 5;

  num step1Result = findWeightOfFuel(spgr1: spgr);

  num step2Result = findGallons(step1Result, lbs1: lbs);

  num step3Result = findTotalWeight(step1Result, lbs1: lbs);


  // print(finalResult);
  print(step1Result.toStringAsFixed(2)); // outputs: 18.66
  print(step2Result.toStringAsFixed(1)); // outputs: 0.3
  print(step3Result.toStringAsFixed(1)); // outputs: 93.3
}

num findWeightOfFuel({required num spgr1}) {
  return (spgr1 * 9.33);
}

num findGallons(num step1result, {required num lbs1}) {
  return (lbs1 / step1result);
}

num findTotalWeight(num step2Result, {required num lbs1}) {
  return (step2Result * lbs1);
}

// 感谢您的帮助,正如你所看到的,我已经尝试过,但我需要一些帮助! 感谢您的时间和考虑!

I worked on this and its day 3 now. I'm super frustrated because I'm trying to write cleaner code.

(Part 1): Trying to make it into a class

// Getting instances of the method but no method output.

(Part 2): Code works on Dartpad

// Need this in a class and how to call on methods for use in other classes.

// (Part 1: class FindPoundsToGallons is not working) // tried several different ways

// I want to be able to call on and reuse the classes across the application is what I'm going for...

// I can't seem to call the correct output when I try them in a class.

class FindPoundsToGallons {
  late num userInputSpgr;
  late num userInputLbsIssued;
  double numCon833 = 8.33;

  FindPoundsToGallons({
    required this.userInputSpgr,
    required this.userInputLbsIssued,
    required this.numCon833,
  });
}

void poundsToGallons() {
  num spgr = 1.5; // sample number
  num lbs = 15; // sample number
  num step1Result = findWeightOfFuel(spgr1: spgr);
  num step2Result = findGallons(step1Result, lbs1: lbs);
  num step3Result = findTotalWeight(step1Result, lbs1: lbs);
  num step4Result = finalResult(step1Result,step2Result,step3Result);
// ToDo: Tell user rounded down to the nearth tenth place

  // print(finalResult);
  print(step1Result.toStringAsFixed(2));
  print(step2Result.toStringAsFixed(1));
  print(step3Result.toStringAsFixed(1));
}

num findWeightOfFuel({required num spgr1}) {
  return (spgr1 * numCon833);
}

num findGallons(num step1result, {required num lbs1}) {
  return (lbs1 / step1result);
}

num findTotalWeight(num step2Result, {required num lbs1}) {
  return (step2Result * lbs1);
}
String finalResult(num step2Result, {required num lbs1}) {
  return ('$lbs / $spgi * numCon833 = $finalResult');
}

// Tried examples of:

FindPoundsToGallons q1 = FindPoundsToGallons();

// to return

q1.findWeightOfFuel //

Pounds to Gallons Multistep class methods

Part 2: // This part works on Dartpad / I need it in a class.

// Formula: Pounds To Gallons
// Trying to put this into a class for later and trying to call on the methods but I keep getting instances of it without being able to see the output when I have them in a class I try to create. 


void main() {
  num spgr = 2;
  num lbs = 5;

  num step1Result = findWeightOfFuel(spgr1: spgr);

  num step2Result = findGallons(step1Result, lbs1: lbs);

  num step3Result = findTotalWeight(step1Result, lbs1: lbs);


  // print(finalResult);
  print(step1Result.toStringAsFixed(2)); // outputs: 18.66
  print(step2Result.toStringAsFixed(1)); // outputs: 0.3
  print(step3Result.toStringAsFixed(1)); // outputs: 93.3
}

num findWeightOfFuel({required num spgr1}) {
  return (spgr1 * 9.33);
}

num findGallons(num step1result, {required num lbs1}) {
  return (lbs1 / step1result);
}

num findTotalWeight(num step2Result, {required num lbs1}) {
  return (step2Result * lbs1);
}

// Thanks for the assistance AND as you can see I have tried, but I need some help!
Thank you for your time and consideration!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文