Dart语言要点记录:
一、变量
声明
- var :有初始值类型固定,没有初始值类型任意
//此处没给aa赋值 相当于aa类型为dynamic ,类型可以随意改变
var aa;
aa = 2;
aa = "dart";
- dynamic: 动态任意类型,编译阶段不检测类型
//dynamic
dynamic bb=10;
bb="sdad";
- Object:动态任意类型,编译阶段检查类型
Object cc=10;
cc="2323";
默认值
- 未初始化的变量或对象默认值null
私有变量
- 无private 等字段需要将方法或者属性前加(_)
Object cc=10;
cc="2323";
var abc = ABC(11,22,333);
abc.ccc;
//如果ABC定义在其他文件内,此处报错
abc._aaa;
常量的关键字
final:运行时确定其值的,但一旦赋值后就无法再更改,可作为类中的属性常量,复制后不在改变
class ABC {
final int _aaa;
int _bbb;
int ccc;
ABC(this._aaa, this._bbb, this.ccc);
}
const:编译时确定值,类级别用static const、方法内部定义常量则不需要
常量的关键字
- 基本数据类型
-
- number
-
- String
-
- Bool
-
- List
-
- Set
-
- Map
-
- Runes
-
- Symbol
num? aaa;
int? bbb;
double? ccc;
String s1 = 'Single quotes work well for string literals.';
bool ddd=false;
var list = [1, 2, 3];
List list1= [2,3,4];
var halogens = {'yiibai.com', 'chlorine', 'bromine', 'iodine', 'astatine'};
var names = <String>{};
Set<String> names1 = {};
//编译时常量的集合
final constantSet = const {
'fluorine',
'chlorine',
'bromine',
'iodine',
'astatine',
};
//映射 Map
var gifts = {
// Key: Value
'first': 'partridge',
'second': 'turtledoves',
'fifth': 'golden rings'
};
abc(){
var gift = gifts['first'];
var gifts1 = Map<String,String>();
gifts1['first'] = 'partridge';
gifts1['second'] = 'turtledoves';
gifts1['fifth'] = 'golden rings';
var gifts2 = {'first': 'partridge'};
gifts2['fourth'] = 'calling birds';
}
//符文 Runes 对象是一个32位的字符对象。它可以把文字转换成 符号表情 或 特定的文字
cde(){
const str = '😇';
print(str); // 😇
print(str.length); // 2
Runes runesStr = str.runes;
print(runesStr); // (128519)
print(runesStr.length); // 1
// Runes 可以将 UTF-32 字符集表示的内容转成符号
Runes runes = new Runes('\u{1f680}');
String str2 = new String.fromCharCodes(runes);
print(str2); // 🚀
}
二、函数
- 常规定义
//无返回值
void funcName1(){
}
//有返回值
String func2(){
return "111";
}
//返回、参数 可省略类型,类js可进行类型推到
firstMethod( a,[String? b,String? c]){
return "";
}
//支持缩写语法
int func3(int a, int b) => a+b;
add(a,b) =>a+b ;
add1(a,b) =>{a+b} ;
//函数内部函数 -- c/c++中的函数指针 相当于函数变量
Function funcInner(int a,{String? b,String? c}){
//相当于匿名函数
add(a,b) =>a+b ;
//函数指针,可作为参数传递,类似于callback
var func=(int a,int b)=>a+b;
add(1,2);
func(1,2);
//可以直接将函数指针作为变量返回
return func;
}
- dart语言函数唯一标识 “函数名称”,不支持参数方式重载
void firstMethod(){
}
//Error: 'firstMethod' is already declared in this scope
//不支持参数方式重载
//void firstMethod(int a){
//}
- 可用可变参数实现扩展函数功能。且更方便灵活
-
- 可变参数必须初始化可为? [] 和{}区别如下
//
void main(){
firstMethod(10);
firstMethod(10,"bis");
firstMethod(10,"bis","eee");
firstMethod1(10,b:"bis");
firstMethod1(10,c:"bis");
firstMethod1(10,c:"bis",b: "eee");
}
//[] 可变参不需要写参数名称,可传可不传,参数顺序必须正确
void firstMethod(int a,[String? b,String? c]){
}
void firstMethod1(int a,{String? b,String? c}]){
}
- 支持类似于c/c++ 别名
//函数的类型是Function类型,typedef就是给Function取个别名,相当于进行一次简单的封装
//泛指所有参数一样的方法,有扩展参数
typedef MyFunc(int a, int b);
func3(int a,int b){
print("func4:${a+b}");
}
//有扩展参数的也可以,但是不能给扩展传值,typedef只认识参数一样的,多传报错
func4(int a,int b,{int? c,int? d}){
print("func4:${a+b}");
}
//可作为参数传递,
func5(int a,int b,MyFunc func){
func(a,b);
}
void main(){
// print('Hello, World!');
// firstMethod(10);
// firstMethod(10,"bis");
// firstMethod(10,"bis","eee");
MyFunc myFunc;
myFunc=func4;
myFunc(1,3);
func5(3,4,myFunc);
func5(3,4,func4);
}
三、流程控制
- if else
- for,forEach,for-in
int colors=["black","red","blue]
for(var color in colors){
print(color);
}
- while,do-while
- break,continue
- assert(断言):与throw类似,但是此处false必断
四、异常
说明:dart 提供异常Error、Exception ,可自定义异常只要使用throw抛出的对象都可以算异常,异常可捕获可不捕获 on 表示异常命中 其他与java类似
void main(){
try {
abcd();
} on Exception catch (e, s) {
//on 代表命中Exception
//e代表错误,s代表错误堆栈信息
print(s);
}
try {
abcd();
} catch (e) {
//e代表错误,s代表错误堆栈信息
print(s);
}
}
abcd(){
var abc=CDE();
throw abc;
}
五、对象
对象
- 私有写法 "_"
- 构造函数定义的多种形式
- 私有属性get set
- 私有方法定义使用
class A{
int a;
double b;
String c;
int _x=1;//定义私有成员
A(this.a, this.b, this.c);
//重定向构造函数 rux
A.AA(this.a, this.b, this.c);
A.BB(int a,double b,String c):this(a,b,c);
A.CC(int a,double b,String c):a=a,b=b,c=c;
//可以直接这样写带默认参数的
A.DD():a=1,b=2,c="";
A.EE(this.a,{ this.b=2, this.c="232323"});
//私有类可以定义getset 供外部调用
int get x => _x;
set x(int value) {
_x = value;
}
//私有方法
int _innerFun(){
return 1;
}
int getInnerFun(){
_innerFun();
}
}
封装、继承、多态、混入
- extends :继承类实现其内部接口方法
abstract class A{
String name;
int age;
A(this.name, this.age);
A.A(this.name, this.age);
//抽象方法定义规则,要求子类必须继承
void marry();
void printer(){
print("object");
}
}
class B extends A {
B(super.name, super.age);
@override
void marry() {
print("marry");
}
}
- implements 其内部的所有方法和属性,一般父类中只有待实现的接口时使用
abstract class A{
String name;
int age;
A(this.name, this.age);
A.A(this.name, this.age);
//抽象方法定义规则,要求子类必须继承
void marry();
void printer(){
print("object");
}
}
class C implements A{
@override
void marry() {
print("marry");
}
@override
late int age;
@override
late String name;
@override
void printer() {
}
}
- mixin(混入) 与java不同它可以把所有想继承的类混入当前类中,关键字with
- 优先级 1、with>extends>implements ,2、越靠后实现的优先级越高,理解为覆盖
- implements 会检测当前类 继承类,with和extends的所有类是否有实现implements中的所有方法或者属性没有则当前类必须实现
class A {
doPrint() {
print("print A");
}
}
class B {
doPrint() {
print("print B");
}
}
mixin C {
doPrint() {
print("print C");
}
}
mixin D {
doPrint() {
print("print D");
}
}
class E {
doPrint() {
print("print E");
}
}
class F {
doPrint() {
print("print F");
}
}
//集成检测关系链 G>D>C>B>F>E 前面的实现了后面接口的则不会报错
class G extends B with C, D implements E, F {
doPrintG() {
print("print G");
}
}
六、泛型
- 用法与java相似
//泛型
void testT <T>(T a,String b){
List<T> stringList=List.empty();
var map=Map();
}
class KK<T>{
late T data;
}