Class Dart类和对象

Class Dart类和对象,class,dart,Class,Dart,我正在寻找一些关于Dart类和对象的指导 我已经看了很多关于飞镖课程的视频,但我仍然很难理解它们的用途和使用方法 我一直在做我自己的小项目——一个动物年龄转换工具——来理解Dart的核心概念。我的目标是利用这段代码构建一个flifter应用程序。学习曲线的所有部分 我已经在“类”(和对象)这个话题上呆了几天了,我希望有人能看看我当前的代码,并提出一种逻辑方法将类合并到其中。我认为这种方法可能有助于澄清问题 import "dart:math"; import "da

我正在寻找一些关于Dart类和对象的指导

我已经看了很多关于飞镖课程的视频,但我仍然很难理解它们的用途和使用方法

我一直在做我自己的小项目——一个动物年龄转换工具——来理解Dart的核心概念。我的目标是利用这段代码构建一个flifter应用程序。学习曲线的所有部分

我已经在“类”(和对象)这个话题上呆了几天了,我希望有人能看看我当前的代码,并提出一种逻辑方法将类合并到其中。我认为这种方法可能有助于澄清问题

import "dart:math";
import "dart:io";

var humanMap = {
    'bear': 2,
    'chicken': 5.33, 
    'cat': 3.2, 
    'dog': 3.64, 
    'elephant': 1.14,
    'human': 1,
    'rabbit': 8.89,
    }; // human age ratio Map.

int age(String promptAge) {
    print(promptAge);
    int answerAge = int.parse(stdin.readLineSync());
    return answerAge; 
    // the prompt function for userInput <int> 'promptAge'
}

String prompt(String promptAnimal) {
    print(promptAnimal);
    String answerAnimal = stdin.readLineSync();
    return answerAnimal; 
    // the prompt function for userInput <String> 'promptAnimal'
}

double ratio(String ageRatio) {
    print(ageRatio);
    double answerRatio = double.parse(stdin.readLineSync());
    return answerRatio; 
    // the prompt function for userInput <double> 'animalRatio'
}

void ageCalc() {
    print("In ${howOld} human years - ");
    humanMap.forEach((key, val) {
        print("- a ${key}'s age would be approx. ${(howOld * val).toStringAsFixed(1)} ");
}); // function to return a list of animals in the map + howOld userInput and calculates the relative animal age.
}

int howOld = 0;
String newAnimal = "";
double animalRatio = 0.0;

void main() {
    String animalName = prompt('choose from - ${humanMap.keys}: '); 
    // calls the promptAnimal function, displays the animal names from the Map
    
    // checks if userInput for animalName contains Key in humanMap - if true 
    if (humanMap.containsKey(animalName)) {
        print("your choice is ${animalName}");
        // calls the promptAnimal function, assigns userInput - age - to howOld.
        howOld = age("In human years what is the age of the ${animalName}? ");
        ageCalc();
    } else {
        newAnimal = prompt("Type '$animalName' again to add to the list."); // adds a new animal key to animalName
        animalRatio = ratio("Enter a number in decimal format, eg 2.3: "); // adds ratio value to animalRatio
        print("$newAnimal and $animalRatio");
        humanMap.putIfAbsent(newAnimal, () => animalRatio); // puts the new animal:ratio (k,v) pair into the humanMap.
        print(humanMap);
        main();
        }
}
导入“dart:math”;
导入“dart:io”;
var humanMap={
"熊":2,,
“鸡”:5.33,
“猫”:3.2,
“狗”:3.64,
“大象”:1.14,
"人":1,,
“兔子”:8.89,
}; // 人类年龄比率图。
整数(字符串提示){
印刷(提示);
int answerAge=int.parse(stdin.readLineSync());
回答;
//用户输入“promptAge”的提示函数
}
字符串提示(字符串提示){
印刷品(promptAnimal);
字符串answerAnimal=stdin.readLineSync();
回答动物;
//用户输入“promptAnimal”的提示函数
}
双倍比率(字符串Agratio){
印刷品(ageRatio);
double answerRatio=double.parse(stdin.readLineSync());
回报率;
//用户输入“animalRatio”的提示函数
}
void ageCalc(){
打印(“以${howOld}人年为单位-”;
humanMap.forEach((键,val){
打印(“-a${key}的年龄大约为${(howled*val).toStringAsFixed(1)}”);
});//函数返回地图中的动物列表+howOld userInput并计算相对动物年龄。
}
int howOld=0;
字符串newAnimal=“”;
双动物比率=0.0;
void main(){
字符串animalName=prompt('choose from-${humanMap.keys}:');
//调用PrompAnimal函数,显示地图中的动物名称
//检查animalName的userInput是否包含humanMap中的密钥-如果为true
if(humanMap.containsKey(animalName)){
打印(“您的选择是${animalName}”);
//调用promptAnimal函数,将userInput-age-分配给howled。
howOld=年龄(“在人类年龄中,${animalName}的年龄是多少?”);
ageCalc();
}否则{
newAnimal=prompt(“再次键入“$animalName”以添加到列表。”);//向animalName添加新的动物键
animalRatio=ratio(“输入十进制格式的数字,如2.3:”);//将比率值添加到animalRatio
打印($newAnimal和$animalRatio);
putIfAbsent(newAnimal,()=>animalRatio);//将新的动物:比率(k,v)对放入humanMap。
印刷(人形地图);
main();
}
}
提前谢谢

卡尔