Flutter Getting Started The first step -Dart language (b)

Flutter Getting Started The first step -Dart language (b)

The operator 1.Dart

2. Abnormal

3. Classes and Objects

The operator 1.Dart

1.1 ToSei

void main() 
{
var a = 7 ;
var b = 2;
print(a~/b);//输出1
}

1.2 cascade operation
when we conduct a series of operations on a single object, you can use a cascade. In the dart, the cascade can ..
be cascaded. In the following example:

class Person {
    String name;
    String country;
    void setCountry(String country){
      this.country = country;
    }
    String toString() => 'Name:$name\nCountry:$country';
}
void main() {
  Person p = new Person();
  p ..name = 'Wang'
    ..setCountry('China');
  print(p);
}

1.3 determine statement IF
DART judgment and sentence java syntax is the same.

if(a>b)
{
	print(a); 
}
else
{
	print(b);
}

1.4for cycle
dart in java for loop and consistent. The following example

for(int i = 0; i<3; i++) {
  print(i);
}

1.5 foreach loop and in

void main() {
var collection = [0, 1, 2];

collection.forEach((x) => print(x));//forEach的参数为Function,这里的x就像是我们在java中的冒号左面的那个变量,但直接放在了匿名函数中作为了形参。
  
 for(var x in collection) {
   print(x);
 }
  
}

1.6switch_case
DART in switch_case and java is basically the same, but to explain here

1.7?. And? ?
?. Meaning (question mark dot operator) if the symbol is represented on the left is null, the code symbols will block execution right
? ? (Double interrogation operator) representative of the meaning of the symbol on the left if the value is null, will be the value on the right as a default value is assigned to the sign left of the symbol.
In the following example:

var boolean = list[i]?.contains(1) ?? false;

.
.
.

2. Exceptional
Unlike the Java language is checked exception Dart concept does not exist, all the exceptions can be understood as an exception, meaning that the program will not be mandatory for programmers to handle exceptions when running the Java language, but if an exception occurs, the interrupt program execution.
Dart Error and Exception divided into two types.
Interestingly in Dart we can throw any type of anomaly, or even a word, but generally we do not do that.
In dart, we use the type of abnormality on + + catch (Exception objects) to capture the exception, in the following example:

main() {
  try {
    test();
  } on ArrayIndexOutOfBoundEXception catch(e){
    print(e);
  }on Error catch(e){
    print(e);
  }
}
test(){
  throw ArrayIndexOutOfBoundEXception("下标越界");
}

If we can not handle this exception, we can re-throw:

main() {
  try {
    test();
  } on ClassError catch(e){
    print(e);
    rethrow;
  }
}
test(){
  throw ClassError("类型异常");
}

Java and the same thing is that we can finally do the final processing.
.
.
.

3. classes and objects
dart in all classes inherit from object
declaration dart class and java similar. Labeled with a class to class. With new to create objects. In the absence of rewriting the constructor, there is a default no-argument constructor.
Definitions 3.1 category

class Point {
  num x;
  num y;
  num z;
}

void main() {
  var point = new Point();
  print(point.x);
}

3.2 static resource
dart in static resources to modify the static keyword, which is the restriction of the basic and java similar, one difference is that dart through the static resources can only be accessed by class name, the object can not be accessed, and the java objects You can also access the class static resources.

Constructor 3.3 naming
syntax Dart does not support function overloading, you can not use the same language like Java constructor overloading. Therefore, when the need to use multiple constructors, use the name of this form constructor

void main(){
  var a = new NumberTest(2,3);
  var b = new NumberTest.cons1();
  var c = new NumberTest.cons2(5);
  a.display();
  b.display();
  c.display();
}

class NumberTest{
  num a;
  num b;

  NumberTest(this.a, this.b){} //构造函数
  NumberTest.cons1(){//命名构造函数1
    this.a = 0;
    this.b = 0;
  }
  NumberTest.cons2(this.a){//命名构造函数2
    this.a = a;
    this.b = 0;
  }
  void display(){
    print("数据:${this.a},${this.b}");
  }

3.4 abstract class
in Dart classes and interfaces are unified, class is the interface
if you want to rewrite some of the features, then you can inherit a class
if you want to perform certain functions, you can achieve a class

Abstract keyword used to define an abstract class, an abstract class and can not be instantiated
abstract method does not require the keyword directly to a semicolon; end to
the following sample code:

abstract class Shape { // 定义了一个 Shape 类/接口
    num perimeter(); // 这是一个抽象方法,不需要abstract关键字,是隐式接口的一部分。
}

class Rectangle implements Shape { // Rectangle 实现了 Shape 接口
    final num height, width; 
    Rectangle(num this.height, num this.width);  // 紧凑的构造函数语法
    num perimeter() => 2*height + 2*width;       // 实现了 Shape 接口要求的 perimeter 方法
}

class Square extends Rectangle { // Square 继承 Rectangle
    Square(num size) : super(size, size); // 调用超类的构造函数
}

This article on here, see Part II

Published 47 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41525021/article/details/104296464