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
switch statement
break 和 continue
break out of the current loop structures.
When the end of cycles continue, for the next cycle.
Through java
Assertion
abnormal
Throw an exception
Catch the exception
class
Constructor
The default constructor
named constructor
redirect Constructor
Constants Constructor
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
When inherited, the constructor call flow
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
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
Abstract method
Abstract class
Implicit Interface
Extended class
Operator rewritable
Enumerated type
mixin inheritance pattern
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();
}