flutter开发Flutter圈子

Dart教程(二):基本语法

2018-12-18  本文已影响9人  康小曹

一、几个基本概念


二、基础数据类型

num和string相互转换
// String -> int
var one = int.parse('1');
assert(one == 1);

// String -> double
var onePointOne = double.parse('1.1');
assert(onePointOne == 1.1);

// int -> String
String oneAsString = 1.toString();
assert(oneAsString == '1');

// double -> String
String piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == '3.14');
字符串单双引号的使用
// 双引号和单引号可以相互替代,没有区别
var string1 = "caoxiaokang";
print(string1);

// 三引号支持换行
String multibleLineString = '''hello
world''';
String singleLineString = "caoxk";

// 使用+拼接字符串
print(multibleLineString + "\n" + singleLineString);

// 相连字符串直接拼接
var newString = "hello" 'world';
print(newString); // helloworld

// $的使用
var string = "$multibleLineString" + "\n" + "$singleLineString";
print(string);

输出:

[Running] dart "/Users/caoxk/Demo/dart/main.dart"
caoxiaokang
hello
world
caoxk
helloworld
hello
world
caoxk

三、特殊数据类型

数组:List

xxx

字典:Map

xxx

Unicode:Runes

xxx

标志符:Symbols

xxx


四、函数

1.函数的基本格式
2.函数的缩写
isNoble(atomicNumber) {
  return _nobleGases[atomicNumber] != null;
}

对于只有一个表达式的方法,你可以选择 使用缩写语法来定义:

bool isNoble(int atomicNumber) => _nobleGases[atomicNumber] != null;

这个 => *expr* 语法是 { return *expr*; } 形式的缩写。=> 形式 有时候也称之为 胖箭头 语法。

注意:在箭头 (=>) 和冒号 (;) 之间只能使用一个 表达式 – 不能使用 语句。 例如:你不能使用 if statement,但是可以 使用条件表达式 conditional expression

3.函数当做参数传递

代码:

// function 也是对象,是function类
var func = (message) => '!!!${message.toUpperCase()}!!!';
var result = func('hello'); //断点打在此处
print(result);

控制台中打印func变量:

func
Closure: (dynamic) => String
call:Closure
hashCode:236919621
runtimeType:Type ((dynamic) => String)

func在runtime中的类型是:参数为dynamic,返回值为String

备注:
这里如果将参数'hello'改成int类型的1,则会运行出错,报错类似于OC中的找不到方法的错误

可选命名参数

可选命名参数的意思是在定义函数时,可以指定参数的名字。这样做的好处是使得函数易于理解,可读性强。
使用方法:

type funcName({type param1, type param2}) {
......
};

举个🌰:
命名的函数及其调用方法:

   // named params
   void func1({bool param1, bool param2}) {
     print('${param1}' + '-' + '$param2');
   };
   func1(param1: true, param2: false);

未命名的函数及其调用方法:

  // not named params
  void func2(bool param1, bool param2) {
     print('${param1}' + '-' + '$param2');
  }
  func2(true, false);

如果直接调用命名函数则会报错:


报错.png
可选位置参数

备注
可选命名参数和可选位置参数不能同时存在,例如:

@required

?????
Required is defined in the meta package. Either import package:meta/meta.dart directly, or import another package that exports meta, such as Flutter’s package:flutter/material.dart.
?????
使用@required修饰的参数书序必传参数,如果不传,编译器会报错

const Scrollbar({Key key, @required Widget child})

类型判定操作符

The as, is, and is! operators are handy for checking types at runtime.

Operator Meaning
as Typecast (also used to specify library prefixes)
is True if the object has the specified type
is! False if the object has the specified type

The result of obj is T is true if obj implements the interface specified by T. For example, obj is Object is always true.

类:class

重点:

1.点方法"obj?.property/method()"等价于如果是如果不是null,则执行.后面的语句
2.有些类提供了常量构造函数。使用常量构造函数 可以创建编译时常量,要使用常量构造函数只需要用 const 替代 new 即可:

var p = const ImmutablePoint(2, 2);

3.可以使用 Object 的 runtimeType 属性来判断实例 的类型,该属性 返回一个 Type 对象。

print('The type of a is ${a.runtimeType}');

4.Dart 是一个面向对象编程语言,同时支持基于 mixin 的继承机制。 每个对象都是一个类的实例,所有的类都继承于 Object.基于 Mixin 的继承 意味着每个类(Object 除外) 都只有一个超类,一个类的代码可以在其他 多个类继承中重复使用。

构造函数
上一篇 下一篇

猜你喜欢

热点阅读