“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Introduction to the
Dart has some very special operators that you can customize in addition to ordinary arithmetic operators. Today we’ll take a look at dart’s special operators.
Common operator
Common operators are easy to explain, namely addition, subtraction, multiplication and division, logical operators, comparison operators, bitwise operators and so on.
These operators are no different from operators in other languages, so I won’t go into detail here. Let’s look at some examples of common operators:
a++ a + b a = b a == b c ? a : b assert(2 == 2); assert(2 ! = 3); assert(3 > 2); assert(2 < 3);Copy the code
Type test operator
Dart type testers are similar to the JAVA instance of operation. They are as, IS, and IS!
Where IS is the type judgment operator, and AS is the type conversion operator, also known as cast.
For the following statement, it returns true if obj is a subclass of T or implements an interface to T.
obj is T
Copy the code
The following statement will always return true:
obj is Object?
Copy the code
The AS operator in DART represents a type conversion, after which the method of the corresponding type can be used. As follows:
(student as Student).firstName = 'Bob';
Copy the code
So the question is, what’s the difference between writing it up here and writing it down here?
if (student is Person) {
// Type check
student.firstName = 'Bob';
}
Copy the code
In the first, an error is reported if student is empty or not an instance of student, while in the second, it is not.
Conditional operator
Dart also supports conditional operators, most commonly ternary operators:
condition ? expr1 : expr2
Copy the code
Indicates that exPR1 is returned if condition is true, expr2 is returned otherwise.
Dart provides us with a very simple nullify operator:
expr1 ?? expr2
Copy the code
If exPR1 is empty, select expr2. Here’s an example:
String playerName(String? name) => name ?? 'Guest';
Copy the code
Cascade symbol
The cascading symbol is.. Or? . To perform a sequence of operations on the same object, we can create an object at the same time, assign a value to the object:
var paint = Paint() .. color = Colors.black .. strokeCap = StrokeCap.round .. StrokeWidth = 5.0;Copy the code
The above code is equivalent to:
var paint = Paint(); paint.color = Colors.black; paint.strokeCap = StrokeCap.round; Paint. StrokeWidth = 5.0;Copy the code
If the object may be empty, we can prefix the first cascade operator with? If the object is empty, the subsequent cascading operations will not take place, as shown below:
var paint = Paint() ? . color = Colors.bla .. strokeCap = Stroke .. StrokeWidth = 5.0;Copy the code
Custom operators in class
You can override operators like C++ in dart. You can do things like add, subtract, multiply and divide between objects.
For example, in the following class, we define the addition and subtraction operations between classes:
class Vector { final int x, y; Vector(this.x, this.y); Vector operator +(Vector v) => Vector(x + v.x, y + v.y); Vector operator -(Vector v) => Vector(x - v.x, y - v.y); Void main() {final v = Vector(2, 3); // Operator == and hashCode not shown. final w = Vector(2, 2); assert(v + w == Vector(4, 5)); assert(v - w == Vector(0, 1)); }Copy the code
Custom operators are conveniently decorated with the operator keyword.
conclusion
That’s the introduction and use of the dart operators.
This article is available at www.flydean.com
The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!
Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!