# Dart 语言简易教程(六)

Dart 语言简易教程(一)： http://www.jianshu.com/p/8a62b1a2fd75
Dart 语言简易教程(二)： http://www.jianshu.com/p/b2153a32dd8b
Dart 语言简易教程(三)： http://www.jianshu.com/p/6d2495a0d3d7
Dart 语言简易教程(四): http://www.jianshu.com/p/fdd046a6dc82

# Dart 语言简易教程(六)

## 对象

Dart 是一种面向对象的语言，并且支持基于mixin的继承方式。
Dart 语言中所有的对象都是某一个类的实例。所有的类有同一个基类--Object。

var jsonData = JSON.decode('{"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);

var p = new Point(2, 2);

// Set the value of the instance variable y.
p.y = 3;

// Get the value of y.
assert(p.y == 3);

// Invoke distanceTo() on p.
num distance = p.distanceTo(new Point(4, 4));

// If p is non-null, set its y value to 4.
p?.y = 4;

var p = const ImmutablePoint(2, 2);

print('The type of a is \${a.runtimeType}');

### 实例化变量(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.
}

class Point {
num x;
num y;
}

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.
}

### 构造函数(Constructors)

class Point {
num x;
num y;

Point(num x, num y) {
// There's a better way to do this, stay tuned.
this.x = x;
this.y = y;
}
}

this关键字指向了当前类的实例。

class Point {
num x;
num y;

// Syntactic sugar for setting x and y
// before the constructor body runs.
Point(this.x, this.y);
}

#### 构造函数不能继承(Constructors aren’t inherited)

Dart 语言中，子类不会继承父类的命名构造函数。如果不显式提供子类的构造函数，系统就提供默认的构造函数。

#### 命名的构造函数(Named constructors)

class Point {
num x;
num y;

Point(this.x, this.y);

// Named constructor
Point.fromJson(Map json) {
x = json['x'];
y = json['y'];
}
}

##### 调用父类的非默认构造函数

1. initializer list
2. superclass’s no-arg constructor
3. main class’s no-arg constructor

class Person {
String firstName;

Person.fromJson(Map data) {
print('in Person');
}
}

class Employee extends Person {
// Person does not have a default constructor;
// you must call super.fromJson(data).
Employee.fromJson(Map data) : super.fromJson(data) {
print('in Employee');
}
}

main() {
var emp = new Employee.fromJson({});

// Prints:
// in Person
// in Employee
if (emp is Person) {
// Type check
emp.firstName = 'Bob';
}
(emp as Person).firstName = 'Bob';
}

in Person
in Employee
##### 初始化器列表(Initializer list)

import 'dart:math';

class Point {
final num x;
final num y;
final num distanceFromOrigin;

Point(x, y)
: x = x,
y = y,
distanceFromOrigin = sqrt(x * x + y * y);
}

main() {
var p = new Point(2, 3);
print(p.distanceFromOrigin);
}

3.605551275463989

#### 重定向构造函数

class Point {
num x;
num y;

// The main constructor for this class.
Point(this.x, this.y) {
print("Point(\$x, \$y)");
}

// Delegates to the main constructor.
Point.alongXAxis(num x) : this(x, 0);
}

void main() {
var p1 = new Point(1, 2);
var p2 = new Point.alongXAxis(4);
}

Point(1, 2)
Point(4, 0)

#### 静态构造函数(Constant constructors)

1. 将所有的类的变量定义为final 类型。
2. 定义const 类型的构造函数。
class ImmutablePoint {
final num x;
final num y;
const ImmutablePoint(this.x, this.y);
static final ImmutablePoint origin =  const ImmutablePoint(0, 0);
}

### 工厂构造函数(Factory constructors)

factory 关键字的功能，当实现构造函数但是不想每次都创建该类的一个实例的时候使用。

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);
}
}
}
void main() {
var p1 = new Logger("1");
p1.log("2");

var p2 = new Logger("11");
p2.log("21");

}

2
21

## 方法(Methods)

### Getters and Setters

get()set()方法是Dart 语言提供的专门用来读取和写入对象的属性的方法。

get()set()的例子：

class Rectangle {
num left;
num top;
num width;
num 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;
}

main() {
var rect = new Rectangle(3, 4, 20, 15);
assert(rect.left == 3);
rect.right = 12;
assert(rect.left == -3);
}

Observatory listening on http://127.0.0.1:33439
Unhandled exception:
'file:///opt/program/Dart/0910.dart': Failed assertion: line 20 pos 10: 'rect.left == -3' is not true.
#0      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:27)
#1      _AssertionError._checkAssertion (dart:core-patch/errors_patch.dart:34)
#2      main (file:///opt/program/Dart/0910.dart:20:10)
#3      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:261)

Process finished with exit code 255

### 抽象方法(Abstract methods)

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...
}
}

### 重载操作

1 2 3 4
< + []
> / ^ []=
<= ~/ & ~
>= * << ==
% >>

1 2 3 4
< + []
> / ^ []=
<= ~/ & ~
>= * << ==
% >>

class Vector {
final int x;
final int y;
const Vector(this.x, this.y);

/// Overrides + (a + b).
Vector operator +(Vector v) {
return new Vector(x + v.x, y + v.y);
}

/// Overrides - (a - b).
Vector operator -(Vector v) {
return new Vector(x - v.x, y - v.y);
}
}

main() {
final v = new Vector(2, 3);
final w = new Vector(2, 2);

// v == (2, 3)
assert(v.x == 2 && v.y == 3);

// v + w == (4, 5)
assert((v + w).x == 4 && (v + w).y == 5);

// v - w == (0, 1)
assert((v - w).x == 0 && (v - w).y == 1);
}

### 抽象类（Abstract classes）

// This class is declared abstract and thus
// can't be instantiated.
abstract class AbstractContainer {
// ...Define constructors, fields, methods...

void updateChildren(); // Abstract method.
}

class SpecializedContainer extends AbstractContainer {
// ...Define more constructors, fields, methods...

void updateChildren() {
// ...Implement updateChildren()...
}

// Abstract method causes a warning but
// doesn't prevent instantiation.
void doSomething();
}

### 隐式的接口(Implicit interfaces)

// 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(who) => 'Hello, \$who. I am \$_name.';
}

// An implementation of the Person interface.
class Imposter implements Person {
// We have to define this, but we don't use it.
final _name = "";

String greet(who) => 'Hi \$who. Do you know who I am?';
}

greetBob(Person person) => person.greet('bob');

main() {
print(greetBob(new Person('kathy')));
print(greetBob(new Imposter()));
}

Observatory listening on http://127.0.0.1:33001
Hello, bob. I am kathy.
Hi bob. Do you know who I am?

Process finished with exit code 0

### 扩展类(Extending a class)

class Television {
void turnOn() {
_illuminateDisplay();
_activateIrSensor();
}
// ...
}

class SmartTelevision extends Television {
void turnOn() {
super.turnOn();
_bootNetworkInterface();
_initializeMemory();
}
// ...
}

### 枚举类型(Enumerated types)

enum Color {
red,
green,
blue
}

assert(Color.red.index == 0);
assert(Color.green.index == 1);
assert(Color.blue.index == 2);

List<Color> colors = Color.values;
assert(colors[2] == Color.blue);

enum Color {
red,
green,
blue
}
// ...
Color aColor = Color.blue;
switch (aColor) {
case Color.red:
print('Red as roses!');
break;
case Color.green:
print('Green as grass!');
break;
default: // Without this, you see a WARNING.
print(aColor);  // 'Color.blue'
}

### 使用’mixins‘ 功能给类添加新的功能

mixins是一种方便重用一个类的代码的方法。

with用法的实例：

class Musician extends Performer with Musical {
// ...
}

class Maestro extends Person
with Musical, Aggressive, Demented {
Maestro(String maestroName) {
name = maestroName;
canConduct = true;
}
}

### 类变量和方法(Class variables and methods)

#### 类常量

class Color {
static const red =
const Color('red'); // A constant static variable.
final String name;      // An instance variable.
const Color(this.name); // A constant constructor.
}

main() {
assert(Color.red.name == 'red');
}

#### 类方法

import 'dart:math';

class Point {
num x;
num y;
Point(this.x, this.y);

static num distanceBetween(Point a, Point b) {
var dx = a.x - b.x;
var dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
}

main() {
var a = new Point(2, 2);
var b = new Point(4, 4);
var distance = Point.distanceBetween(a, b);
assert(distance < 2.9 && distance > 2.8);
}