Dart

2019-10-08  本文已影响0人  CaptainRoy
print('Hello World');
String name = 'roy';
int age = 18;
double height = 1.88;
print('${name},${age},${height}');

变量不能更改类型,使用dynamic声明则可以改变类型

var name = 'roy';
name = 'WL';
print(name);
dynamic name = 'Roy';
name = 18;
print(name);
var name = 'Roy';
print(name.runtimeType);
final country = "中国";
const province = "湖北";
var one = int.parse("111");
var two = double.parse("12.22");
print("${one} , ${one.runtimeType}");
print("${two} , ${two.runtimeType}");
var one = 123;
var two = 123.123;
var oneStr = one.toString();
var twoStr = two.toString();
print("${oneStr} , ${oneStr.runtimeType}");
print("${twoStr} , ${twoStr.runtimeType}");
var isFlag = false;
print("${isFlag} , ${isFlag.runtimeType}");

if (isFlag == true) {
    print("是真");
}
var letters = ['a','b','c','d'];
print("${letters} , ${letters.runtimeType}");

List<int> numbers = [1,2,3,4];
print("${numbers} , ${numbers.runtimeType}");
var letterSet = {'a','b','c','d'};
print("${letterSet} , ${letterSet.runtimeType}");

Set<int> numberSet = {1,2,3,4};
print("${numberSet} , ${numberSet.runtimeType}");
var info = {'name' : 'roy', 'age' : 18};
print("${info} , ${info.runtimeType}");

Map<String,Object> infoMap = {'height' : 1.89 , 'address' : '北京市'};
print("${infoMap} , ${infoMap.runtimeType}");
var num = 7;
print(num / 3); // 除法 2.3333333333333335
print(num ~/ 3); // 整除 2
print(num % 3); // 取余 1
  var name1 = "roy";
  var name2 = null;
  name1 ??= "lily";
  print(name1);
  print(name2);

  var temp = "why";
  temp = null;
  var tempName = temp ?? "kobe";
  print(tempName);
for (var i = 0; i < 5; i++) {
    print(i);
}

var names = ["roy","lily","leo"];
for (var name in names) {
    print(name);
}
var direction = "east";
  switch (direction) {
    case "east":
      print("东面");
      break;
    case "south":
      print("南面");
      break;
    case "west":
      print("西面");
      break;
    case "north":
      print("北面");
      break;
    default:
      print("其他方向");
  }
上一篇下一篇

猜你喜欢

热点阅读