一 變量和數據類型javascript
1,定義變量的方法:var 變量名; var 變量名=**; 數據類型 變量名; 數據類型 變量名=***;java
2,基本的數據類型:api
int:整數類型,(var a=1; or int a=1;)angular2
double : 浮點類型,(var a=1.1; or double a=1.1;)async
num:便可整數類型,也能夠是浮點數類型(num a = 1.1)ide
String:字符串類型 (var name="xiaoming"; or String name = 'xiaoming';)函數
字符串可使用¥符號進行與其餘的是數據類型進行拼接(String str = "a = $a"; or cc='${bb}';)ui
length:查看字符串的長度(int b = a.length)this
bool:布爾類型,就只有兩個值,false and truespa
List:列表類型(var a=[1,2,3,4]; or List a=[1,2,'qq',1.2]),列表數據類型裏面能夠放其餘的數據類型
經過索引進行取值,從0開始:var element = list[0];
length:查看列表的長度,也就是幾個元素(int length = list.length;)
若是隻想存一種數據類型的數據,那要藉助泛型(List<String> list3 = <String>['a', 'b', 'c'];)其餘的數據類型就會報錯
Map:至關於字典類型(Map map = {'Apple' : '蘋果', 'Banana' : '香蕉', 'Peach' : '桃子', 1 : '1', true : 0};)
根據key取值(String value = map['Apple'];)
Map 的鍵和值的數據類型也能夠是指定的(Map<int, String> map = <int, String>{1 : 'a', 2 : 'b', 3 : 'c'};)若是放入其餘的數據類型就會報錯
二,if判斷
var year = 18; 單支判斷: if (year>=20){ print('大了'); } 雙支判斷 if (year>=20){ print('大了'); }else{ print('小了'); } 多支判斷 if (year > 20) { print('大了'); } else if (year == 18) { print('正好'); }else{ print('小了'); }
三,循環語句;
for (var object in flybyObjects) { print(object); } for (int month = 1; month <= 12; month++) { print(month); } while (year < 2016) { year += 1; }
四,函數
int test(int n){ if (n>0){return n;}else{ return -1; } }
五,導入模塊
// Importing core libraries import 'dart:async'; import 'dart:math'; // Importing libraries from external packages import 'package:angular2/angular2.dart'; // Importing files import 'path/to/my_other_file.dart';
六,練習
// import 'dart:io'; int test(int n){ if (n>0){return n;}else{ return -1; } } var name="fang"; var year=18; var bot=4.10; var li = [name,year,bot]; void main() { print(li); if (year>=20){ print('大了'); }else{ print('小了'); } var aa; aa=test(0); print(aa); for (var i in li){ print(i); } for (int i =1;i<10;i++){ var aa =''; for (int j = i;j<10;j++){ var bb=i*j; var cc; if (bb > 9){ cc='${bb}'; }else{ cc='${bb} '; } aa = '${aa} ${i}*${j}=${cc}'; } int a=aa.length; print(a); // print(aa); } print(11%2); for (var i=1;i<101;i++){ if (i%2==0){ print(i); } } for (var i=1;i<101;i++){ if (i%2==1){ print(i); } } }
七,類
class Test{ String name; Test(this.name){ name=this.name; print(name); } int test_1(int n){ return n*2; } void test_3(){ print(this.name); if (this.name=="fang"){ print('yes'); }else{ print('no'); } } } // 單繼承 class Test_1 extends Test{ int age; Test_1(this.age,String name):super(name); void test_4(){ var cc = "${name}今年${age}歲"; print(cc); } } // 多繼承 class Test_2{ int astronauts; // Test_2(this.astronauts){ // astronauts=this.astronauts; // print(astronauts); // } void describeCrew() { print('Number of astronauts: $astronauts'); } } class TestMixins extends Test_1 with Test_2 { int year; TestMixins(this.year,age,name,astronauts):super(age,name); void test_mixins(){ print("test"); } } // 隱含api class Test_3{ String name; Test_3(this.name){ name = this.name; } void test_3(){ } } class TestImplement implements Test_3{ @override String name; TestImplement(this.name){ name=this.name; print(name); } @override void test_3(){ print(this.name); if (this.name=="fang"){ print('yes'); }else{ print('no'); } } } // 抽象類 abstract class Test_4{ void describe(); void describeWithEmphasis() { print('========='); describe(); print('========='); } } class Test_5 extends Test_4{ @override void describe(){ print('test'); } } void main() { // class var test_2=new Test('fang'); int aa = test_2.test_1(10); print(aa); test_2.test_3(); // 單繼承 var test_5 = new Test_1(18,'fang'); test_5.test_4(); test_5.test_3(); // 多繼承 var testmixins = new TestMixins(2000,18,'fang',1); testmixins.test_4(); testmixins.describeCrew(); testmixins.test_mixins(); // 隱含api var testimp = new TestImplement('fang'); testimp.test_3(); //抽象類 var test5 = new Test_5(); test5.describeWithEmphasis(); }