Flutter Learning (II) of Dart depth grammar

table of Contents

  • Flow control statements
  • abnormal
  • class
  • Generics

Flow control statements

The if statement

void displayName0(String name){
  if(name==null){
    print("2020");
  }else{
    print(name);
  }
}

void displayName1(String name){
  print(name==null?"2019":name);
}

void displayName2(String name){
  print(name??"2019");
}

void main() {
  var name = null;
  displayName1(name);
}

for loop

a. the standard for loop
b.forEach
c.for in ...

void main() {
  var list = [1,2,3,4];
  
  //标准for循环打印数据
  for(var i=0;i<list.length;i++){
    print(list[i]);
  }
  
  //forEach打印数据 
  list.forEach((e)=> print(e));
  
  //for...in 打印数据
  for(var item in list){
    print(item);
  }
}

while loop

Here Insert Picture Description

switch statement

Here Insert Picture Description

break 和 continue

break out of the current loop structures.
When the end of cycles continue, for the next cycle.
Through java
Here Insert Picture Description

Assertion

Here Insert Picture Description

abnormal

Here Insert Picture Description

Throw an exception

Here Insert Picture Description

Catch the exception

Here Insert Picture Description
Here Insert Picture Description

class

Constructor

Here Insert Picture Description
The default constructor
Here Insert Picture Description
named constructor
Here Insert Picture Description
redirect Constructor
Here Insert Picture Description
Constants Constructor
Here Insert Picture Description

class ConstBean{
  final int x;
  final int y;
  
  const ConstBean(this.x,this.y);
}

void main() {
  ConstBean a = const ConstBean(1,2);
  ConstBean b = const ConstBean(1,2);
  assert(identical(a,b));
  ConstBean c =  ConstBean(1,2);
  // a 和 c并不是一个实力,有力c的实例化,没有增加const关键字
  assert(identical(a,c));
  print('hello');
}

Plant constructors
Here Insert Picture Description

When inherited, the constructor call flow

Here Insert Picture Description

class Person {
  Person.fromJson(Map data){
    print("Person");
  }
}

class Student extends Person{
  
   Student.fromJson(Map data):super.fromJson(data){
    print("Student");
  }
}

void main() {
   Student s = Student.fromJson({});
}

method

Here Insert Picture Description

class Point{
  num x,y;
  Point(this.x,this.y);
  num distanceTo(Point d){
      var dx = d.x - x;
      var dy = d.y - y;
      return sqrt(dx*dx+dy*dy);
  }
}

void main() {
  Point p = Point(1,2);
  Point p1 = Point(4,5);
  print (p1.distanceTo(p)); 
}

getter 和 setter

Here Insert Picture Description

Abstract method

Here Insert Picture Description

Abstract class

Here Insert Picture Description

Implicit Interface

Here Insert Picture Description

Extended class

Here Insert Picture Description

Operator rewritable

Here Insert Picture Description

Enumerated type

mixin inheritance pattern

Here Insert Picture Description

abstract class Animal{}
abstract class Bird{}

mixin Walker{
  void walk(){
    print("I am walking");
  }
}

mixin Flyer{
  void fly(){
    print ("I am flying");
  }
}

class Parrot extends Bird with Walker,Flyer{}


void main() {
  Parrot parrot = Parrot();
  parrot.fly();
  parrot.walk();
}

Static member

Here Insert Picture Description

The use of generics

Here Insert Picture Description

Use a collection of literals

Here Insert Picture Description

Type parameters constructors

Here Insert Picture Description

Defining extends parameterized types using

Here Insert Picture Description

Generic method

Here Insert Picture Description

Published 98 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/dirksmaller/article/details/103989760