在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and constIf you never intend to change a variable, use Note: Instance variables can be Here’s an example of creating and setting a final variable: final name = 'Bob'; // Without a type annotation
// name = 'Alice'; // Uncommenting this causes an error
final String nickname = 'Bobby';
Use const bar = 1000000; // Unit of pressure (dynes/cm2)
const double atm = 1.01325 * bar; // Standard atmosphere
The // Note: [] creates an empty list.
// const [] creates an empty, immutable list (EIL).
var foo = const []; // foo is currently an EIL.
final bar = const []; // bar will always be an EIL.
const baz = const []; // baz is a compile-time constant EIL.
// You can change the value of a non-final, non-const variable,
// even if it used to have a const value.
foo = [];
// You can't change the value of a final or const variable.
// bar = []; // Unhandled exception.
// baz = []; // Unhandled exception.
For more information on using -----------------------------------------------------------Default parameter valuesYour function can use void doStuff(
{List<int> list = const [1, 2, 3],
Map<String, String> gifts = const {
'first': 'paper',
'second': 'cotton',
'third': 'leather'
}}) {
print('list: $list');
print('gifts: $gifts');
}
------------------------------------------------------------The main() functionEvery app must have a top-level // Run the app like this: dart args.dart 1 test
void main(List<String> arguments) {
print(arguments);
assert(arguments.length == 2);
assert(int.parse(arguments[0]) == 1);
assert(arguments[1] == 'test');
}
------------------------------------------------------------Operatorsif null ?? cascade .. Arithmetic operators/ Divide print(5 / 2 == 2.5); // Result is a double Type test operatorsas Typecast Assignment operatorsAs you’ve already seen, you can assign values using the // Assign value to a
a = value;
// Assign value to b if b is null; otherwise, b stays the same
b ??= value;
Conditional expressions
Cascade notation (..)Cascades ( Consider the following code: querySelector('#confirm') // Get an object.
..text = 'Confirm' // Use its members.
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'));
The first method call, The previous example is equivalent to: var button = querySelector('#confirm');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) => window.alert('Confirmed!'));
You can also nest your cascades. For example: final addressBook = (new AddressBookBuilder()
..name = 'jenny'
..email = '[email protected]'
..phone = (new PhoneNumberBuilder()
..number = '415-555-0100'
..label = 'home')
.build())
.build();
Be careful to construct your cascade on a function that returns an actual object. For example, the following code fails: var sb = new StringBuffer();
sb.write('foo')
..write('bar'); // Error: method 'write' isn't defined for 'void'.
The Note: Strictly speaking, the “double dot” notation for cascades is not an operator. It’s just part of the Dart syntax. Other operators?. Conditional member access Like ., but the leftmost operand can be null; example: foo?.bar selects property bar from expression foo unless foo is null (in which case the value of foo?.bar is null) ------------------------------------------------------------ExceptionsYour Dart code can throw and catch exceptions. Exceptions are errors indicating that something unexpected happened. If the exception isn’t caught, the isolate that raised the exception is suspended, and typically the isolate and its program are terminated. In contrast to Java, all of Dart’s exceptions are unchecked exceptions. Methods do not declare which exceptions they might throw, and you are not required to catch any exceptions. Dart provides Exception and Error types, as well as numerous predefined subtypes. You can, of course, define your own exceptions. However, Dart programs can throw any non-null object—not just Exception and Error objects—as an exception. ThrowHere’s an example of throwing, or raising, an exception: throw new FormatException('Expected at least 1 section');
You can also throw arbitrary objects: throw 'Out of llamas!';
Because throwing an exception is an expression, you can throw exceptions in => statements, as well as anywhere else that allows expressions: void distanceTo(Point other) =>
throw new UnimplementedError();
------------------------------------------------------------ClassesDart is an object-oriented language with classes and mixin-based inheritance. Every object is an instance of a class, and all classes descend from Object. Mixin-based inheritance means that although every class (except for Object) has exactly one superclass, a class body can be reused in multiple class hierarchies.
To create an object, you can use the
var jsonData = jsonDecode('{"x":1, "y":2}');
// Create a Point using Point().
var p1 = new Point(2, 2);
// Create a Point using Point.fromJson().
var p2 = new Point.fromJson(jsonData);
Dart 2 note: You can omit the Some classes provide constant constructors. To create a compile-time constant using a constant constructor, use var p = const ImmutablePoint(2, 2);
Dart 2 note: You can omit the Constructing two identical compile-time constants results in a single, canonical instance: var a = const ImmutablePoint(1, 1);
var b = const ImmutablePoint(1, 1);
assert(identical(a, b)); // They are the same instance!
To get an object’s type at runtime, you can use Object’s print('The type of a is ${a.runtimeType}');
Instance variablesHere’s how you declare instance variables: class Point {
num x; // Declare instance variable x, initially null.
num y; // Declare y, initially null.
num z = 0; // Declare z, initially 0.
}
All uninitialized instance variables have the value All instance variables generate an implicit getter method. Non-final instance variables also generate an implicit setter method. For details, see Getters and setters. class Point {
num x;
num y;
}
void main() {
var point = new Point();
point.x = 4; // Use the setter method for x.
assert(point.x == 4); // Use the getter method for x.
assert(point.y == null); // Values default to null.
}
If you initialize an instance variable where it is declared (instead of in a constructor or method), the value is set when the instance is created, which is before the constructor and its initializer list execute. ConstructorsDeclare a constructor by creating a function with the same name as its class (plus, optionally, an additional identifier as described in Named constructors). The most common form of constructor, the generative constructor, creates a new instance of a class: class Point {
num x, y;
Point(num x, num y) {
// There's a better way to do this, stay tuned.
this.x = x;
this.y = y;
}
}
The Note: Use The pattern of assigning a constructor argument to an instance variable is so common, Dart has syntactic sugar to make it easy: class Point {
num x, y;
// Syntactic sugar for setting x and y
// before the constructor body runs.
Point(this.x, this.y);
}
Default constructorsIf you don’t declare a constructor, a default constructor is provided for you. The default constructor has no arguments and invokes the no-argument constructor in the superclass. Named constructorsUse a named constructor to implement multiple constructors for a class or to provide extra clarity: class Point {
num x, y;
Point(this.x, this.y);
// Named constructor
Point.origin() {
x = 0;
y = 0;
}
}
Remember that constructors are not inherited, which means that a superclass’s named constructor is not inherited by a subclass. If you want a subclass to be created with a named constructor defined in the superclass, you must implement that constructor in the subclass. Invoking a non-default superclass constructorBy default, a constructor in a subclass calls the superclass’s unnamed, no-argument constructor. The superclass’s constructor is called at the beginning of the constructor body. If an initializer list is also being used, it executes before the superclass is called. In summary, the order of execution is as follows:
If the superclass doesn’t have an unnamed, no-argument constructor, then you must manually call one of the constructors in the superclass. Specify the superclass constructor after a colon ( class Employee extends Person {
Employee() : super.fromJson(getDefaultData());
// ···
}
Note: When using Warning: Arguments to the superclass constructor do not have access to Initializer listBesides invoking a superclass constructor, you can also initialize instance variables before the constructor body runs. Separate initializers with commas. // Initializer list sets instance variables before
// the constructor body runs.
Point.fromJson(Map<String, num> json)
: x = json['x'],
y = json['y'] {
print('In Point.fromJson(): ($x, $y)');
}
Warning: The right-hand side of an initializer does not have access to During development, you can validate inputs by using Point.withAssert(this.x, this.y) : assert(x >= 0) {
print('In Point.withAssert(): ($x, $y)');
}
Initializer lists are handy when setting up final fields. Redirecting constructorsSometimes a constructor’s only purpose is to redirect to another constructor in the same class. A redirecting constructor’s body is empty, with the constructor call appearing after a colon (:). class Point {
num x, y;
// The main constructor for this class.
Point(this.x, this.y);
// Delegates to the main constructor.
Point.alongXAxis(num x) : this(x, 0);
}
Constant constructorsIf your class produces objects that never change, you can make these objects compile-time constants. To do this, define a class ImmutablePoint {
static final ImmutablePoint origin =
const ImmutablePoint(0, 0);
final num x, y;
const ImmutablePoint(this.x, this.y);
}
Factory constructorsUse the The following example demonstrates a factory constructor returning objects from a cache: class Logger {
final String name;
bool mute = false;
// _cache is library-private, thanks to
// the _ in front of its name.
static final Map<String, Logger> _cache =
<String, Logger>{};
factory Logger(String name) {
if (_cache.containsKey(name)) {
return _cache[name];
} else {
final logger = new Logger._internal(name);
_cache[name] = logger;
return logger;
}
}
Logger._internal(this.name);
void log(String msg) {
if (!mute) print(msg);
}
}
Note: Factory constructors have no access to To invoke a factory constructor, you use the var logger = new Logger('UI');
logger.log('Button clicked');
MethodsMethods are functions that provide behavior for an object. Getters and settersGetters and setters are special methods that provide read and write access to an object’s properties. Recall that each instance variable has an implicit getter, plus a setter if appropriate. You can create additional properties by implementing getters and setters, using the class Rectangle {
num left, top, width, height;
Rectangle(this.left, this.top, this.width, this.height);
// Define two calculated properties: right and bottom.
num get right => left + width;
set right(num value) => left = value - width;
num get bottom => top + height;
set bottom(num value) => top = value - height;
}
void main() {
var rect = new Rectangle(3, 4, 20, 15);
assert(rect.left == 3);
rect.right = 12;
assert(rect.left == -8);
}
With getters and setters, you can start with instance variables, later wrapping them with methods, all without changing client code. Note: Operators such as increment (++) work in the expected way, whether or not a getter is explicitly defined. To avoid any unexpected side effects, the operator calls the getter exactly once, saving its value in a temporary variable. Abstract methodsInstance, getter, and setter methods can be abstract, defining an interface but leaving its implementation up to other classes. Abstract methods can only exist in abstract classes. To make a method abstract, use a semicolon (;) instead of a method body: abstract class Doer {
// Define instance variables and methods...
void doSomething(); // Define an abstract method.
}
class EffectiveDoer extends Doer {
void doSomething() {
// Provide an implementation, so the method is not abstract here...
}
}
Calling an abstract method results in a runtime error. Overridable operatorsYou can override the operators shown in the following table. For example, if you define a Vector class, you might define a Abstract classesUse the Implicit interfacesEvery class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface. A class implements one or more interfaces by declaring them in an // A person. The implicit interface contains greet().
class Person {
// In the interface, but visible only in this library.
final _name;
// Not in the interface, since this is a constructor.
Person(this._name);
// In the interface.
String greet(String who) => 'Hello, $who. I am $_name.';
}
// An implementation of the Person interface.
class Impostor implements Person {
get _name => '';
String greet(String who) => 'Hi $who. Do you know who I am?';
}
String greetBob(Person person) => person.greet('Bob');
void main() {
print(greetBob(new Person('Kathy')));
print(greetBob(new Impostor()));
}
Here’s an example of specifying that a class implements multiple interfaces: class Point implements Comparable, Location {
// ···
}
Extending a classUse class Television {
void turnOn() {
_illuminateDisplay();
_activateIrSensor();
}
// ···
}
class SmartTelevision extends Television {
void turnOn() {
super
|
请发表评论