You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.3 KiB
Dart

void basic_build_in_types() {
// Numbers - (int, double) num
// Booleans - true 或 false
// Lists - Collections of items (aka arrays)
// Sets - Collection of unique items (Dart 2.2 正式引入)
// Maps - Collections with associated Key Value Pairs
// Strings - "Hello!" (single and double quotes)
// Runes - unicode character points
// Symbols - #symbol (simbolic metadata)
int x = 1;
int hex = 0xFA;
print("int: $x, hex int: $hex");
double y = 1.1;
double exponents = 1.42e5;
print("double: $y, exponents: $exponents");
// String <=> int
int one = int.parse('1');
assert(one == 1);
String oneAsString = 1.toString();
assert(oneAsString == '1');
// String <=> double
double onePointOne = double.parse('1.1');
assert(onePointOne == 1.1);
String piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == '3.14');
// Bitwise 運算
assert((3 << 1) == 6); // 0011 << 1 == 0110
assert((3 >> 1) == 1); // 0011 >> 1 == 0001
assert((3 | 4) == 7); // 0011 | 0100 == 0111
// Check for zero.
int hitPoints = 0;
assert(hitPoints <= 0);
print("hitPoints <= 0? ${hitPoints <= 0}");
print("\nDart Strings:");
String str1 = "It's a string with double quotes";
String str2 = 'It\'s a string with single quote';
String str3 = """
It's a long string with triple double quotes
這是一個很長的字串
""";
print(str1);
print(str2);
print(str3);
const aConstNum = 0;
const aConstBool = true;
const aConstString = 'a constant string';
var aNum = 0;
var aBool = true;
var aString = 'a string';
const aConstList = [1, 2, 3];
const validConstString = '$aConstNum $aConstBool $aConstString';
// const invalidConstString = '$aNum $aBool $aString $aConstList';
print("validConstString: $validConstString");
print("$aNum $aBool $aString $aConstList, ${aConstList[0]}");
print("\nDart List:");
// List & Generics(泛型)
List<String> fruits = ['bananas', 'grapes', 'oranges'];
print(fruits);
print("fruits.length: ${fruits.length}");
print("fruits[1]: ${fruits[1]}");
fruits[1] = "apples";
print(fruits);
fruits.sort();
print(fruits);
fruits = fruits.map((String fruit) {
return fruit.substring(0, fruit.length - 1);
}).toList();
print(fruits);
print("\nDart Set:");
// Set
Set<String> halogens = {'fluorine', 'chlorine', 'bromine', 'iodine', 'astatine'}; // 鹵元素 氟氯溴碘砈
print(halogens);
var elements = <String>{};
elements.add('fluorine');
elements.addAll(halogens);
print("\nDart Map:");
// Map
Map gifts = {
// Key: Value
'first': 'partridge',
'second': 'turtledoves',
'fifth': 'golden rings'
};
print("gifts.length: ${gifts.length}");
print("gifts['first']: ${gifts['first']}");
// 惰性氣體
Map<int, String> nobleGases = {
2: 'helium',
10: 'neon',
18: 'argon',
};
print(nobleGases.containsValue("argon"));
}
void basic_function() {
print("\nDart Function:");
// Function
dynamic_add(a, b) {
return a + b;
}
print(dynamic_add(1, 2));
print(dynamic_add(20.0, 40.0));
print(dynamic_add("a", "b"));
int add(int a, int b) {
return a + b;
}
Function func = add;
var result = func(20, 30);
print("func result is $result");
Map ops = {
'add': add,
'sub': (int a, int b) => a - b,
};
result = ops['sub'](6, 10);
print("ops sub result: $result");
}