成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Dart編程語言入門學(xué)習(xí)

ChanceWong / 1513人閱讀

摘要:數(shù)據(jù)類型不可變不可變

1.數(shù)據(jù)類型 Boolean
void main(){
  bool isTrue = true;
  bool isFalse = false;

  print("Hello".isEmpty);
}
Number
void main(){
  num a = 10;
  a = 12.5;

  int b = 20;
//  b = 20.5;

  double c = 10.5;
//  c = 30;

  print(b + c);
  print(b - c);
  print(b * c);
  print(b / c);
  print(b ~/ c);
  print(b % c);

  print(0.0 / 0.0);

  print(b.isEven);
  print(b.isOdd);
  int d = 11;
  print(d.isEven);
  print(d.isOdd);

  int e = -100;
  print(e.abs());

  double f = 10.5;
  print(f.round());
  print(f.floor());
  print(f.ceil());

  print(f.toInt());
  print(d.toDouble());

}

String
void main(){
  String str1 = "Hello";//""
  String str2 = """Hello
                  Dart""";//"""
  print(str2);

//  String str3 = "Hello 
 Dart";
  String str3 = r"Hello 
 Dart";
  print(str3);

  String str4 = "This is my favorite language";
  print(str4 + "New");
  print(str4 * 5);
  print(str3 == str4);
  print(str4[1]);

  int a = 1;
  int b = 2;
  print("a + b = ${a + b}");
  print("a = $a");

  print(str4.length);
  print(str4.isEmpty);

  print(str4.contains("This"));
  print(str4.substring(0,3));
  print(str4.startsWith("a"));
  print(str4.endsWith("ge"));

  var list = str4.split(" ");
  print(list);

  print(str4.replaceAll("This", "That"));
}

Const
void main(){
  var a;
  print(a);

  a = 10;
  print(a);

  a = "Hello Dart";
  print(a);

  var b = 20;
  print(b);

  final c = 30;
//  c = 50;

  const d = 20;
//  d = 50;

}
List
void main(){
  var list1 = [1,2,3,"Dart",true];
  print(list1);
  print(list1[2]);
  list1[1] = "Hello";
  print(list1);

  var list2 = const [1,2,3];  //不可變
//  list2[0] = 5;

  var list3 = new List();

  var list = ["hello","dart"];
  print(list.length);
  list.add("New");
  print(list);

  list.insert(1, "Java");
  print(list);

  list.remove("Java");
  print(list);

//  list.clear();
//  print(list);

  print(list.indexOf("dart1"));
  list.sort();
  print(list);

  print(list.sublist(1));

  list.forEach(print);
}

Map
void main(){
  var map1 = {"first":"Dart",1:true,true:"2"};
  print(map1);

  print(map1["first"]);
  print(map1[true]);
  map1[1] = false;
  print(map1);

  var map2 = const {1:"Dart",2:"Java"};//不可變
//  map2[1] = "Python";

  var map3 = new Map();

  var map = {"first":"Dart","second":"Java","third":"Python"};
  print(map.length);
//  map.isEmpty;

  print(map.keys);
  print(map.values);

  print(map.containsKey("first"));
  print(map.containsValue("C"));

  map.remove("third");
  print(map);

  map.forEach(f);

  var list = ["1","2","3"];
  print(list.asMap());
}

void f(key,value){
  print("key=$key,value=$value");
}

dynamic
void main(){
  var a;
  a = 10;
  a = "Dart";

  dynamic b = 20;
  b = "JavaScript";

  var list = new List();
  list.add(1);
  list.add("hello");
  list.add(true);
  print(list);
}

2.運(yùn)算符 operator_arithmetic
void main(){
  int a = 10;
  int b = 2;

  print(a + b);
  print(a - b);
  print(a * b);
  print(a / b);
  print(a ~/ b);
  print(a % b);

  print(a++);//11
  print(++a);//12

  print(a--);//11
  print(--a);//10
}

operator_asignment
void main(){
  int a = 10;
  int b = 5;

  b ");print(b);

  a += 2;
  print(a);

  a -= b;
  print(a);

  a *= b;
//  a /= b;
  a ~/= b;
  a %= b;

}
operator_conditional
void main(){
  int gender = 1;
  String str = gender == 0 ");"Male=$gender" :"Female=$gender";
  print(str);

  String a = "Dart";
  String b = "Java";
  String c = a ");print(c);


}

operator_logical
void main(){
  bool isTrue = true;
  print(!isTrue);

  bool isFalse = false;
  print(isTrue && isFalse);
  print(isTrue || isFalse);

  String str = "";
  print(!str.isEmpty);

}

operator_relational
void main(){
  int a = 5;
  int b = 3;

  print(a == b);
  print(a !=b );
  print(a > b);
  print(a < b);
  print(a >= b);
  print(a <= b);

  String strA = "123";
  String strB = "123";
  print(strA == strB);
}

3.控制語句 break_and_continue
void main(){
  var list = [1,2,3];
  for(var item in list){
    if(item == 2) {
//      break;
      continue;
    }
    print(item);
  }

  print("-----");
  var list2 = [4,5,6];
  for(var item1 in list){
    if(item1 == 2){
      break;
    }

    for(var item2 in list2){
      if(item2 == 5){
        break;
      }
      print(item2);
    }
  }
  
}

stmt_assert
void main(){
  String str;
  assert(str != null);
}

stmt_for
void main(){
  var list = [1,2,3,4,5];

  for(var index = 0;index < list.length;index++){
    print(list[index]);
  }

  print("---------");
  for(var item in list){
    print(item);
  }

}

stmt_if
void main(){
  int score = 100;

  if(score >= 90){
    if(score == 100){
      print("完美");
    }else {
      print("優(yōu)秀");
    }
  }else if(score > 60){
    print("良好");
  }else if(score == 60){
    print("及格");
  }else {
    print("不及格");
  }
}
stmt_switch
void main(){
  String language = "Java";

  switch(language){
    case "Dart":
      print("Dart is my favorite");
      break;
    case "Java":
      print("Java is my favorite");
      break;
    case "Python":
      print("Python is my favorite");
      break;
    default:
      print("None");
  }

  switch(language){
    Test:
    case "Dart":
      print("Dart is my favorite");
      break;
    case "Java":
      print("Java is my favorite");
      continue Test;
//      break;
    case "Python":
      print("Python is my favorite");
      break;
    default:
      print("None");
  }
}

stmt_while
void main(){
  int count = 0;

  while(count < 5){
    print(count++);
  }

  print("---$count----");

  do{
    print(count--);
  }while(count > 0 && count < 5);


}
4.方法 function_declaration
void main(List args){
  print(args);

  print(getPerson("張三", 18));

  print(printPerson("李四", 20));
}

int gender = 1;
getPerson(name,age) => gender == 1 ");"name=$name,age=$age":"Test";

printPerson(name,age){
  print("name=$name,age=$age");
}

function_optional_param可選參數(shù)
void main(){

  printPerson("李四");
  printPerson("李四",age: 20);
  printPerson("李四",age: 20,gender: "Male");
  printPerson("李四",gender: "Male");

  printPerson2("張三");
  printPerson2("張三",18);
  printPerson2("張三",18,"Female");
}

printPerson(String name,{int age,String gender}){
  print("name=$name,age=$age,gender=$gender");
}

printPerson2(String name,[int age,String gender]){
  print("name=$name,age=$age,gender=$gender");
}

function_default_param默認(rèn)參數(shù)
void main(){

  printPerson("李四");
  printPerson("李四",age: 20);
  printPerson("李四",gender: "Male");
}

printPerson(String name,{int age = 30,String gender = "Female"}){
  print("name=$name,age=$age,gender=$gender");
}
function_object方法對(duì)象
void main(){
  Function func = printHello;
  func();

  var list = [1,2,3,4];
  list.forEach(print);

  var list2 = ["h","e","l","l","o"];
  print(listTimes(list2, times));
}

void printHello(){
  print("Hello");
}

List listTimes(List list ,String times(str)){
  for(var index = 0;index < list.length;index++){
    list[index] = times(list[index]);
  }

  return list;
}


String times(str){
  return str*3;
}


function_anonymous匿名方法
void main(){

  var func = (str){
    print("Hello---$str");
  };
  func(30);

//  ((){
//    print("Test");
//  })();
  var list2 = ["h","e","l","l","o"];

  var result = listTimes(list2, (str){ return str * 3;});
  print(result);

  print(listTimes2(list2));

}

List listTimes(List list ,String times(str)){
  for(var index = 0;index < list.length;index++){
    list[index] = times(list[index]);
  }

  return list;
}

List listTimes2(List list ){
  var func = (str){ return str * 3;};

  for(var index = 0;index < list.length;index++){
    list[index] = func(list[index]);
  }

  return list;
}

function_closure閉包
void main(){
  var func = a();
  func();
  func();
  func();
  func();

}

a(){
  int count = 0;

//  printCount(){
//    print(count++);
//  }

  return (){
    print(count++);
  };
}

5.面向?qū)ο?/b> class_and_object類與對(duì)象
import "person.dart";

void main() {
    var person = new Person();
    person.name = "Tom";
    person.age = 20;

    print(person.name);
    print(person.address);

    person.work();
}
//person.dart
class Person {
  String name;
  int age;
  final String address = "";

  void work(){
    print("Name is $name,Age is $age,He is working...");
  }
}

calculated_property計(jì)算屬性
void main() {
    var rect = new Rectangle();
    rect.height = 20;
    rect.width = 10;

    print(rect.area);

    rect.area = 200;
    print(rect.width);

}

class Rectangle{
  num width,height;

  num get area => width * height;
      set area(value){
        width = value / 20;
      }
}
constructors構(gòu)造方法
void main() {
    var person = new Person("Tom",20,"Male");
    new Person.withName("John");
    new Person.withAge(20);
}

class Person{
  String name;
  int age;

  final String gender;

  Person(this.name,this.age,this.gender);

  Person.withName(String name){
    this.name = name;
  }

  Person.withAge(this.age);

  void work(){
    print("Work...");
  }
}
const_constructors常量構(gòu)造方法
void main() {
    const person = const Person("Tom",20,"Male");

    person.work();

}

class Person{
  final String name;
  final int age;

  final String gender;

  const Person(this.name,this.age,this.gender);

  void work(){
    print("Work...");
  }
}

factory_constructors工廠構(gòu)造方法
class Logger {
  final String name;

  static final Map _cache = {};

  factory Logger(String name) {
    if (_cache.containsKey(name)) {
      return _cache[name];
    } else {
      final logger = Logger._internal(name);
      _cache[name] = logger;
      return logger;
    }
  }

  Logger._internal(this.name);

  void log(String msg) {
    print(msg);
  }
}
init_list初始化列表
void main() {
    var person = new Person("Tom", 20, "Male");
}

class Person{
  String name;
  int age;

  final String gender;

  Person(this.name,this.age,this.gender);

  Person.withMap(Map map): name = map["name"],gender = map["gender"]{
    age = map["age"];
  }

  void work(){
    print("Work...");
  }
}
static_member靜態(tài)成員
void main() {
  var page = new Page();

  Page.scrollDown();
}

class Page{

  static const int maxPage = 10;

  static int currentPage = 1;

  //下滑
  static void scrollDown(){
    currentPage = 1;
    print("ScrollDown...");
  }

  //上滑
  void scrollUp(){
    currentPage++;
    print("scrollUp...");
  }

}

object_operator對(duì)象操作符
void main() {
//  Person person = new Person();
//  person");"";
//  person = new Person();

//  (person as Person).work();

//  if (person is Person){
//    person.work();
//  }

  new Person()
    ..name = "Tom"
        ..age = 20
        ..work();
}

class Person{
  String name;
  int age;

  void work(){
    print("Work...$name,$age");
  }
}

object_call對(duì)象call
void main() {
    var person = new Person();

    print(person("Test",30));

}

class Person{
  String name;
  int age;

//  void work(){
//    print("Name is $name,Age is $age");
//  }

  String call(String name,int age){
    return "Name is $name,Age is $age";
  }
}

6.面向?qū)ο髷U(kuò)展 繼承
import "person.dart";

void main() {
//  var student = new Student();
//  student.study();
//
//  student.name = "Tom";
//  student.age = 16;
//
//  print(student.isAdult);
//
//  student.run();

  Person person = new Student();
  person.name = "Tom";
  person.age = 18;

  if(person is Student){
    person.study();
  }

  print(person);
}

class Student extends Person{

  void study(){
    print("Student study...");
  }

  @override
  bool get isAdult => age > 15;


  @override
  void run() {
    print("Student run...");
  }

}

class Person {
  String name;
  int age;
  String _birthday;

  bool get isAdult => age > 18;

  void run(){
    print("Person run...");
  }

  @override
  String toString() {
    return "Name is $name,Age is $age";
  }
}

繼承中的構(gòu)造方法
void main() {
  var student = new Student("Tom","Male");

  print(student.name);
}

class Person{
  String name;

  Person(this.name);

  Person.withName(this.name);
}

class Student extends Person{
  int age;

  final String gender;

  Student(String name,String g) : gender = g, super.withName(name);
}

抽象類
void main() {
  var person = new Student();
  person.run();
}

abstract class Person{

  void run();
}

class Student extends Person{
  @override
  void run() {
    print("run...");
  }

}

接口
void main() {
  var student = new Student();
  student.run();
}

//class Person{
//  String name;
//
//  int get age => 18;
//
//  void run(){
//    print("Person run...");
//  }
//}

abstract class Person{

  void run();

}

class Student implements Person{

  @override
  void run() {
    print("Student run...");
  }

}

mixins
void main() {
//  var d = new D();
//  d.a();
}

//class A{
//  void a(){
//    print("A.a()...");
//  }
//}
//
//class B{
//
//  void a(){
//    print("B.a()...");
//  }
//
//  void b(){
//    print("B.b()...");
//  }
//}
//
//class Test{}
//
//
//class C{
//
//  void a(){
//    print("C.a()...");
//  }
//
//  void b(){
//    print("C.b()...");
//  }
//
//  void c(){
//    print("C.c()...");
//  }
//}
//
//class D  extends A with C,B{
//
//}




abstract class Engine{
  void work();
}

class OilEngine implements Engine{
  @override
  void work() {
    print("Work with oil...");
  }

}

class ElectricEngine implements Engine{

  @override
  void work() {
    print("Work with Electric...");
  }

}

class Tyre{
  String name;

  void run(){}
}

class Car = Tyre with ElectricEngine;

class Bus = Tyre with OilEngine;




操作符覆寫
void main() {

  var person1 = new Person(20);
  var person2 = new Person(20);

  print(person1 > person2);

  person1.age;
  print(person1["age"]);

  print(person1 == person2);
}

class Person{
  int age;

  Person(this.age);

  bool operator >(Person person){
    return this.age > person.age;
  }

  int operator [](String str){
    if("age" == str){
      return age;
    }

    return 0;
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
          other is Person &&
              runtimeType == other.runtimeType &&
              age == other.age;

  @override
  int get hashCode => age.hashCode;


}

7.枚舉 enum
void main() {
  var currentSeason  = Season.winter;

  print(currentSeason.index);

  switch(currentSeason){
    case Season.spring:
      print("1-3月");
      break;
    case Season.summer:
      print("4-6月");
      break;
    case Season.autumn:
      print("7-9月");
      break;
    case Season.winter:
      print("10-12月");
      break;
  }
}

enum Season{
  spring,
  summer,
  autumn,
  winter
}

8.泛型 generics
void main() {
  var list = new List();
  list.add(1);

//  var utils = new Utils();
//  utils.put(1);

  var utils = new Utils();
  utils.put(1);
}

class Utils{

   void put(T element){
    print(element);
  }

//  void putString(String element){
//    this.elementStr = element;
//  }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/6818.html

相關(guān)文章

  • flutter的入門實(shí)踐到可開發(fā)

    摘要:繼上一篇關(guān)于的介紹,是仿照微信界面,因?yàn)樽鳛榍岸碎_發(fā),有一定的基礎(chǔ),所有寫起來,也不是很吃力。班門弄斧之作,若有大神見到,敬請(qǐng)指教,有不對(duì)不合理之處,敬請(qǐng)指出我是邇伶貳環(huán)境準(zhǔn)備以系統(tǒng)為例。 flutter的入門記錄 0.前言: flutter 的入門demo 已經(jīng)寫好一個(gè)星期了,只不過一直都沒有整理出博客來。收拾好心情,來整理一下。繼上一篇關(guān)于react-native-wx的介紹,是仿...

    _DangJin 評(píng)論0 收藏0
  • Flutter基礎(chǔ)(三)Dart快速入門

    摘要:工具可以報(bào)告兩種問題警告和錯(cuò)誤。警告只是說明代碼可能無法正常工作,但不會(huì)阻止程序執(zhí)行。中的是一組無序的集合。其中來指定異常類型,來捕獲異常對(duì)象。其中代表只導(dǎo)入指定的部分,代表除了指定的部分都導(dǎo)入。 本文首發(fā)于微信公眾號(hào)「劉望舒」 前言 Dart是Flutter SDK指定的語言,因此要學(xué)習(xí)Flutter,Dart是必須掌握的。關(guān)于Dart可以寫一本書了,這里用一篇文章來介紹下Dart的精...

    helloworldcoding 評(píng)論0 收藏0
  • flutter筆記3:基礎(chǔ)語法、框架、控件

    摘要:是啥是谷歌推出的一套視覺設(shè)計(jì)語言。比如有的可以換皮膚,而每一套皮膚就是一種設(shè)計(jì)語言,有古典風(fēng)呀炫酷風(fēng)呀極簡風(fēng)呀神馬的,而就是谷歌風(fēng),有興趣的同學(xué)可以學(xué)習(xí)了解一下官方原版和中文翻譯版,這是每一個(gè)產(chǎn)品經(jīng)理的必修教材。 flutter環(huán)境和運(yùn)行環(huán)境搭建好之后,可以開始擼碼了,然而當(dāng)你打開VScode,在打開項(xiàng)目文件夾后,擺在你面前的是main.dart被打開的樣子,里面七七八八的已經(jīng)寫好了一...

    draveness 評(píng)論0 收藏0
  • Flutter基礎(chǔ)(二)Flutter最新開發(fā)環(huán)境搭建和Hello World

    摘要:注釋處的方法是程序的入口,使用了符號(hào),這是中單行函數(shù)或方法的簡寫,等價(jià)于如下代碼方法是框架的入口,如果不返回方法,那么執(zhí)行的是一個(gè)控制臺(tái)應(yīng)用。 本文首發(fā)于微信公眾號(hào)「劉望舒」 前言 最近的Google I/O大會(huì)上,F(xiàn)lutter1.5 開始支持移動(dòng)、Web、桌面和嵌入式設(shè)備,從不溫不火的sky一直進(jìn)化到如今熱門的Flutter,F(xiàn)lutter的發(fā)展已經(jīng)超出很多人的想象。我對(duì)跨平臺(tái)技術(shù)一...

    tuomao 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<