dart学习: dart 语法课(youtobe)

基础语法课学习
更新于: 2024-06-14 21:45:13

常用语法列表

课程细节
安装如何安装 dart - 每个语言都要知道如何安装,详情
变量 Dart Variables
void main() {
  // variables
  // strings, numbers, booleans, and doubles
  var name = "John";
  String occupation = "teacher";
  var age = 30;
  var isMarried = true;
  var height = 1.75;

  // dynamic variables
  dynamic greeting = "Hello";
  // greeting = 123; // this is allowed

  // const and final variables
  // const 与 final 变量
  // const 变量的值不能被修改,final 变量的值在初始化之后不能被修改
  // 区别在于 运行时常量 和 编译时常量。
  const PI = 3.14;
  final year = 2024;

  print(name);
  print(occupation);
  print(age);
  print(isMarried);
  print(height);
  print(greeting);

  print(PI); // this will give an error because PI is a constant
  print(year);
}
数据类型 Data Types
void main() {
  // data types in Dart
  // Numbers, Strings, Booleans, Lists, Maps, Sets, Dynamic

  // Strings
  String name = "John";
  print("String: $name");

  // Numbers
  int age = 25;
  double height = 1.75;
  print("Int: $age, Double: $height");

  // Booleans
  bool isMarried = true;
  print("Boolean: $isMarried");

  // Lists
  List<String> fruits = ["Apple", "Banana", "Orange"];
  print("List: $fruits");

  // Maps
  Map<String, int> marks = {"John": 85, "Jane": 90};
  print("Map: $marks");

  // Sets
  Set<int> numbers = {1, 2, 3, 4, 5};
  print("Set: $numbers");

  // Dynamic
  dynamic variable = "Hello";
  variable = 10;
  print("Dynamic: $variable");

  // Get the data type of a variable
  print(name.runtimeType);
}
列表 Dart Lists
void main() {
  var numbers = [1, 2, 3, 4, 5];
  print(numbers);
  print(numbers.length);
  print(numbers[0]);
  print(numbers[numbers.length - 1]);
  numbers.add(6);
  print(numbers);
  numbers.insert(2, 10);
  print(numbers);
  numbers.remove(10);
  print(numbers);
  numbers.removeAt(2);
  print(numbers);
  numbers.clear();
  print(numbers);
}
字典 Dart Maps
void main() {
  // Maps! key-value pairs
  var myMap = {"apple": 2, "banana": 4, "orange": 6};
  print(myMap);
  // show values of the map
  print(myMap.values);
  // show keys of the map
  print(myMap.keys);
  // show length of the map
  print(myMap.length);
  // add a new key-value pair
  myMap["grape"] = 8;
  print(myMap);
  // remove a key-value pair
  myMap.remove("banana");
  print(myMap);
}
循环  Loop
void main() {
  // For loop
  for (int i = 0; i < 5; i++) {
    print(i);
  }

  // For in loop
  List<String> fruits = ["apple", "banana", "orange"];
  for (String fruit in fruits) {
    print(fruit);
  }

  // for each loop
  Map<String, int> scores = {"John": 85, "Jane": 92, "Bob": 78};
  for (String name in scores.keys) {
    print("$name scored ${scores[name]}");
  }


  // while loop
  int j = 0;
  while (j < 5) {
    print(j);
    j++;
  }
}
流程 Logic
void main() {
  // if else statements
  int age = 20;
  if (age >= 18) {
    print("You are old enough to vote.");
  } else {
    print("You are not old enough to vote.");
  }

  // if else if statements
  int temperature = 25;
  if (temperature < 10) {
    print("It's too cold outside.");
  } else if (temperature < 20) {
    print("It's a bit chilly outside.");
  } else if (temperature < 30) {
    print("It's a bit warm outside.");
  } else {
    print("It's hot outside.");
  }
}
函数 Functions
void main() {
  // without return value
  myFunction(){
    print("Hello World");
  }

  myFunction();

  // with return value
  int add(int a, int b){
    return a + b;
  }

  print(add(2, 3));
}
用户输入 UserInput
import 'dart:io';

void main() {
  String? name = stdin.readLineSync();
  print("Hello, $name!");
}
类型转换 string to int
void main() {
  // string to integer conversion
  String str = "1234";
  int num = int.parse(str);
  print(num);

  // string to double conversion
  String str1 = "3.14159";
  double num1 = double.parse(str1);
  print(num1);

  // integer to string conversion
  int num2 = 1234;
  String str2 = num2.toString();
  print(str2);
}
类 Class/OOP
// create Person class
class Person {
  String? name;
  int? age;

  // constructor
  Person(this.name, this.age);

  // method
  void greet() {
    print("Hello, my name is $name and I am $age years old.");
  }
}


void main() {
  // create object of Person class
  Person person1 = Person("John", 25);

  // call method of Person class
  person1.greet();
}