basis
The main function
void main ( ) { }
Declare variables
var a num a int a double a bool a = true
- final b = 0; declaration of variables can only be assigned once
- const a = 1; Constant
- var a = const [1,2]; declare a list of immutable
type of data
type of data
- Number
- Int - integer
- double - float
- String
- Boolean
- List - Array
- Map - value pairs
- Number
Special operators ~ / rounding down
- String Manipulation
''' hello dddd'''
It represents multi-line strings, reserved wrap'hello \n Dart' 会换行
String aa = r''hello \n Dart"
I will not wrap. Will output \ n'abc' * 2 == 'abcabc'
'abc'[1] == 'b'
Interpolation string
" a + b = ${a + b}"
${xx}
is an expression only one variable $ a written interpolation may be omitted
Map key-value pairs
- create
var qq = {'first' : 'Dart' , 'second' : 'java'};
- Create an immutable Map
var q = const { ,,,, }
- Construction create var
q = new Map()
- Acquisition value
qq['frist']
- Cycle pass two values
qq.forEach ( f ); void f( key , value ){.......}
- create
- dynamic type - dynamic type
var a = 10; a = "xxx";
dynamic a = 20; a="xxx";
- Generic Application:
var lis = new List\<dynamic\>();
LIS Such statements may use various types of data
Operators
- = ??
b ??= 10;
B 10 is empty, there is no change value. a~/=b
Similar to a + = b
var c = a ?? b
empty expression is a value of b
function
void main () {}
void return type is a position as not needing void return type- Dart defined function
xxx(){}
js function is definedfunction xx () {}
void fun(String name , int age) {xxxx}
- Optional parameters wording
fun(String name , {int age, String kk}) fun('张三',age:20)
Position is not fixedfun(String name , [int age , String kk]) fun('张三' , 20 )
Fixed position
- Parameter Default
fun(String name , {int age = 30, String kk})
- Anonymous Methods
() {}
Can be assigned to variables or called directly .. it can not be defined directly in the outermost((){})()
Self-adjusting since the implementation of
Object-Oriented
class class-
Create a new class declaration class objects .new be omitted by default all objects inherit from the Object class
class property defaults have getter and setter methods require custom --java
Property class final statement is read-only
class methods can not be overloaded
//举例 class Person { String name; int age; void work(){ print("name is $name , age is $age") }, Person (String name , int age){//构造方法 this.name = name; this.age = age; }; Person (this.name,this.age);//构造方法简写 Person.ddd (){}; //带名称的构造方法 } //调用 var person = new Person();//new Person('Tom'.20)--有构造时 person.name = 'Tom'; person.age = 20; person.work(); //name is Tom , age is 20
Use
_
represented privacy default a file is a library .. Dart Dart set the visibility of visibility to Library (library) is very much a unit of javaclass calculated property
class Rect{ num width,height; num get area => width * height;// area{ return w*h } set area(val) {//给计算属性area赋值时,处理数据,给其他属性赋值 width = val / 20; }; } var re = new Rect(); re.height = 20; re.width = 10; print(re.area); //200
Construction method - a method called when creating an object can set up multiple configurations fancy details above person ..
- Named Constructor
- ---- If constant constructor class is immutable state, it is a compile-time constants corresponding to the object
使用const声明构造方法,并且所有变量都为final,使用const声明对象.可以省略
- Factory construction method
- Factory design pattern similar to the pattern - - plus factory before constructor
- The biggest difference, plant construction, can return objects. --- ???
- Initialization list --- ???
- static declares a static member - ???
- Object operator
?.安全访问成员 person?.work() 对象存在时调用work不存在则不执行
as 类型转换
is is! 是否是指定类型
级联操作 person..name='xx'..age=20..work();
- Object method call
- If the class implements call (), the object class can be used as a method
inherit
- extends -. Visible inherit the parent class attributes and methods subclass can rewrite the parent class method / getter / setter
- Single inheritance / polymorphism
- @override - means that the method is reproducible
- Subclass constructor default constructor calls the parent class unnamed Senate. If the parent does not need to display the call
- Constructor execution sequence: initialization list subclass - parent class constructor - construction of subclasses
Abstract class
- Abstract class abstract, can not be instantiated. It can only be inherited instantiated
interface
- Classes and interfaces are unified