Dart语法学习
- 运算符
- 特殊运算符
var name = "lilei";
name ??= "who";
print(name);
??= - 原来的变量有值时,那么??=不执行
var age = null;
var tmp = age ?? 20;
print(tmp);
?? - 前面的数据有值,那么就使用前面的值;前面值为null,那么就使用后面的值
- 级联运算符
main(List<String> args) {
var p = Person();
p.name = "why";
p.run();
p.eat();
//02_级联
var p2 = Person()
..name = "why"
..eat()
..run();
}
class Person {
String name;
void run() {
print("runing");
}
void eat() {
print("eating");
}
}
- for循环
main(List<String> args) {
//1. 基础for循环
for (var i = 0; i < 10; i++) {
print(i);
}
for (var name in ['a', 'b', 'c', 'd']) {
print(name);
}
}
- 面向对象
- 类的定义
main(List<String> args) {
var p = Person('why', 18);
var s = Student('二年级', 180.0);
}
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
class Student {
String level;
double height;
Student(this.level, this.height);
}
- 类的构造函数
2.1 创建对象
main(List<String> args) {
//1. 创建Person对象
var p0 = Person("namei", 16);
print(p0.toString());
}
class Person {
String name;
int age;
double height;
Person(this.name, this.age);
@override
String toString() {
// TODO: implement toString
return "$name $age $height";
}
}
2.2 Object与dynamic的区别
Object obj = "who";
obj = 1;
print(obj.substring(1));
编译报错
dynamic obj2 = "who";
obj2 = 1;
print(obj2.substring(1));
编译不报错,但是运行时肯能报错
注:定义指定类型变量后,需要赋值其他类型值,使用dynamic,编译时不报错,但是运行时可能会因为找不到方法而报错
2.3 命名构造函数
main(List<String> args) {
//命名构造函数
var p = Person.withNameAgeHeight("lufei", 18, 175.0);
print(p.toString());
var p1 = Person.fromMap({"name":"qiaozhi", "age":19, "height":170.0});
print(p1);
}
class Person {
String name;
int age;
double height;
//命名构造函数
Person.withNameAgeHeight(this.name, this.age, this.height);
Person.fromMap(Map<String, dynamic> map) {
this.name = map["name"];
this.age = map["age"];
this.height = map["height"];
}
@override
String toString() {
// TODO: implement toString
return "$name $age $height";
}
}
- 类的初始化列表
main(List<String> args) {
var p = Person("lufei");
}
class Person {
final String name;
final int age;
Person(this.name, {int age}): this.age = age ?? 10;
}
- 重定向构造函数
main(List<String> args) {
var p = Person("who");
print(p.age);
}
class Person {
String name;
int age;
//构造函数的重定向
Person(String name): this._internal(name, 10);
//命名构造函数
Person._internal(this.name, this.age);
}
- 常量构造函数
main(List<String> args) {
const p1 = Person("why");
const p2 = Person("why");
print(identical(p1, p2));
}
class Person {
final String name;
const Person(this.name);
}
- 工厂构造函数
main(List<String> args) {
//要求:两个对象的name或者color相同,则返回同一个对象
final p1 = Person.withName("why");
final p2 = Person.withName("why");
print(identical(p1, p2));
final p3 = Person.withColor("red");
final p4 = Person.withColor("red");
print(identical(p3, p4));
}
class Person {
String name;
String color;
static final Map<String, Person> _nameCache = {};
static final Map<String, Person> _colorCache = {};
factory Person.withName(String name) {
if (_nameCache.containsKey(name)) {
return _nameCache[name];
} else {
final p = Person(name, "default");
_nameCache[name] = p;
return p;
}
}
factory Person.withColor(String color) {
if (_colorCache.containsKey(color)) {
return _colorCache[color];
} else {
final p = Person("normal", color);
_colorCache[color] = p;
return p;
}
}
Person(this.name, this.color);
}
- 类的setter和getter
main(List<String> args) {
final p = Person();
//直接访问属性
p.name = "who";
print(p.name);
p.setName = "haha";
print(p.getName);
}
class Person {
String name;
//setter
set setName(String name) {
this.name = name;
}
//getter
String get getName {
return name;
}
}
- 类的继承
class Animal {
int age;
Animal(this.age);
}
class Person extends Animal {
String name;
Person(this.name, int age): super(age);
}
- 抽象类
main(List<String> args) {
// final Shape shape = Shape();
}
//抽象类不能实例化;实现工厂构造函数后可以初始化
abstract class Shape {
double getArea();
String getInfo() {
return "形状";
}
// factory Shape() {
// return null;
// }
}
class Rectangle extends Shape {
@override
double getArea() {
// TODO: implement getArea
return 100.0;
}
- 隐式接口
//Dart中没有哪一个关键字是来定义接口的
//没有这些关键字interface/protocol
//当 讲一个类当做接口使用时,那么实现这个接口的类,必须实现这个接口中所有方法
class Runner {
void running() {
}
}
class Flyer {
void flying() {
}
}
class Animal {
void eating() {
}
}
class Surper extends Animal implements Runner, Flyer {
//继承的类调用方法时可以调用super的方法
@override
void eating() {
// TODO: implement eating
super.eating();
}
//实现的类的方法不能调用super的方法
@override
void running() {
// TODO: implement running
}
@override
void flying() {
// TODO: implement flying
}
- mixin混入
main(List<String> args) {
final sm = Surper();
sm.running();
sm.flying();
}
mixin Runner {
void running() {
print("Runner runing");
}
}
mixin Flyer {
void flying() {
print("Flyer flying");
}
}
class Animal {
void eating() {
print("Animal eating");
}
}
//混入的语法:
//1. 定义可混入的类时,不能用class,用mixin
//2.with进行混入
//3. 自己实现了混入类的方法时,调用自己的
class Surper extends Animal with Runner, Flyer {
//继承的类调用方法时可以调用super的方法
@override
void eating() {
// TODO: implement eating
super.eating();
}
void running() {
print("Surper running");
}
}
- 类属性和类方法
class Person {
//成员变量
String name;
//静态属性(类属性)
static String courseTime;
//对象方法
void eating() {
print("eating");
}
//静态方法(类方法)
static void gotoCourse() {
print("去上课");
}
}
- 枚举
main(List<String> args) {
final color = Colors.red;
switch(color) {
case Colors.red:
print("红色");
break;
case Colors.blue:
print("蓝色");
break;
case Colors.green:
print("绿色");
break;
}
print(Colors.values);
print(Colors.red.index);
}
enum Colors {
red,
blue,
green
}
- Dart中库的使用
- 系统库的使用
import 'dart:math'; //核心库不需要导入
//系统库: import ‘dart:库名‘;
main(List<String> args) {
final num1 = 20;
final num2 = 30;
print(min(num1, num2));
}
- 自定义库的使用
/*
* 1. as关键字给库起别名
* 2. 默认情况导入一个库时,导入这个库中所有的公共属性和方法
* 2.1 show:指定导入的内容
* 2.2 hide:因此某个要导入的内容,导入其他内容
* 3. 公共的dart文件的抽取:export
* 4. _用来区分私有方法
*/
// import 'utils/math_utils.dart' as mUtils;
// import 'utils/math_utils.dart' show sum hide mul;
// import 'utils/date_utils.dart';
import 'utils/utils.dart';
main(List<String> args) {
print(sum(20, 30));
// print(mul(20, 30));
print(dateFormat());
min(20, 30);
}
//utils/
//date_utils.dart
String dateFormat() {
return "2020-06-23";
}
//math_utils.dart
int sum(int num1,int num2) {
return num1 + num2;
}
int mul(int num1,int num2) {
return num1 - num2;
}
int min(int num1, int num2) {
return num1 > num2 ? num2 : num1;
}
//utils.dart
export 'math_utils.dart';
export 'date_utils.dart';
- 第三方库的使用
import 'package:http/http.dart' as http;
//导入第三方库 在pubspec.yaml中 dependencies 加入http: ^0.12.1
//安装第三方库 pub get
//await关键字必须在async中才能使用
main(List<String> args) async {
var url = 'https://example.com/whatsit/create';
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
print(await http.read('https://example.com/foobar.txt'));
}
//pubspec.yaml
name: coderwhy
description: a dart libary
dependencies:
http: ^0.12.1
生活如此美好,今天就点到为止。。。
网友评论