First step of getting started with fluent - getting started with Dart language

First step of getting started with fluent - getting started with Dart (2)

1. Operators in dart

2. exceptions

3. Classes and objects

1. Operators in dart

1.1 rounding

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

1.2 cascade operation
When we do a series of operations on a single object, we can use cascading. In dart, cascade can use
To cascade. 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 judgment statement if
dart has the same syntax as java.

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

1.4for cycle
The for loop in dart is also consistent with java. The following example

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

1.5 foreach cycle and in

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

collection.forEach((x) => print(x));//The parameter of forEach is Function. The x here is like the variable to the left of our colon in java, but it is directly put in the anonymous Function as a parameter.
  
 for(var x in collection) {
   print(x);
 }
  
}

1.6switch_case
The switch case and java in dart are basically the same, but there is no explanation here
.
.
.

2. exceptions
Different from Java language, there is no concept of checking exception in Dart. All exceptions can be understood as runtime exceptions of Java language, that is to say, the program will not force the programmer to handle exceptions, but if an exception occurs, the program will interrupt execution.
Dart is mainly divided into two types: Error and Exception.
Interestingly, in Dart, we can throw any kind of exception, even a sentence, but generally we don't.
In dart, we use on + exception type + catch (exception object) to catch exceptions, as shown in the following example:

main() {
  try {
    test();
  } on ArrayIndexOutOfBoundEXception catch(e){
    print(e);
  }on Error catch(e){
    print(e);
  }
}
test(){
  throw ArrayIndexOutOfBoundEXception("Subscript boundary crossing");
}

If we can't handle this exception, we can also re throw it:

main() {
  try {
    test();
  } on ClassError catch(e){
    print(e);
    rethrow;
  }
}
test(){
  throw ClassError("Type exception");
}

The same thing with java is that we can use finally to do the final processing.
.
.
.

3. Classes and objects
All classes in dart inherit from object
The declaration of dart class is similar to java. Mark a class with class. Use new to create objects. When the constructor is not overridden, there will be a parameterless constructor by default.
3.1 definition of class

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

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

3.2 static resources
Static resources in dart are decorated by static keywords, and their constraints are basically similar to those in java. The difference is that static resources in dart can only be accessed through class names, not through objects, and objects in java can also access static resources in classes.

3.3 named constructor
Dart's syntax does not support function overloading and cannot use construction method overloading like the Java language. So when you need to use multiple constructors, you need to use named constructors

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){} //Constructor
  NumberTest.cons1(){//Named constructor 1
    this.a = 0;
    this.b = 0;
  }
  NumberTest.cons2(this.a){//Named constructor 2
    this.a = a;
    this.b = 0;
  }
  void display(){
    print("data:${this.a},${this.b}");
  }

3.4 abstract class
In Dart, classes and interfaces are unified. Classes are interfaces
If you want to override some functions, you can inherit a class
If you want to implement some functions, you can also implement a class

Use the abstract keyword to define an abstract class, and the abstract class cannot be instantiated
Abstract methods don't need keywords, just semicolons
The sample code is as follows:

abstract class Shape { // A Shape class / interface is defined
    num perimeter(); // This is an abstract method that does not require the abstract keyword and is part of an implicit interface.
}

class Rectangle implements Shape { // Rectangle implements the Shape interface
    final num height, width; 
    Rectangle(num this.height, num this.width);  // Compact constructor syntax
    num perimeter() => 2*height + 2*width;       // Implement the perimeter method required by Shape interface
}

class Square extends Rectangle { // Square inherits Rectangle
    Square(num size) : super(size, size); // Calling the constructor of a superclass
}

This is the end of the article. See you next

Published 35 original articles, won praise 12, visited 6548
Private letter follow

Keywords: Java

Added by GuiltyGear on Thu, 13 Feb 2020 15:44:25 +0200