随笔 - 204  文章 - 0  评论 - 15  阅读 - 32万

Dart与Java的语法区别

1. 主函数

  (1) 没有public static

  (2) 命令参数List<String> args

void main() {
}

2. 可以在class外定义变量,方法等

3. 没有public, private, protected关键字

4. 创建对象,new可选

5. 获取值${variableValue}, ${expression}

6. Class中属性默认public,若声明私有,只需在属性名前加_

复制代码
class Bicycle {
  int cadence;
  int _speed = 0;
  int get speed => _speed;
  int gear;

  Bicycle(this.cadence, this.gear);

  void applyBrake(int decrement) {
    _speed -= decrement;
  }

  void speedUp(int increment) {
    _speed += increment;
  }

  @override
  String toString() => 'Bicycle: $_speed mph';
}

void main() {
  var bike = Bicycle(2, 1);
  print(bike);
}
复制代码

7. getter/setter方法

//返回值类型/get/外部可访问属性/方法体
int get speed => _speed
//set/外部可访问属性(值)/方法体
set speed(int value) => _speed = value;

8. 未初始化的变量值均为null

9. 构造函数简化,避免重载

复制代码
import 'dart:math';

class Rectangle {

  Point origin;
  num width;
  num height;

  Rectangle({this.origin = const Point(0, 0), this.width = 0, this.height = 0});

  @override
  String toString() => 'Origin: (${origin.x}, ${origin.y}), width: $width, height: $height';
}

void main() {
  print(Rectangle(origin: const Point(3,4), width: 10, height: 20));
  print(Rectangle(origin: const Point(10,20)));
  print(Rectangle(width: 20));
  print(Rectangle());
}
复制代码

10. 一个dart文件中可以定义多个class,也可以仅仅定义方法

//factory.dart
import 'shapes.dart';

Shape shapeFactory(String type){
  if(type == 'circle') return Circle(2);
  if(type == 'square') return Square(2);
  throw 'Can\'t create $type';
}

11. 字符串可用单引号或者双引号

12. 工厂构造函数 => 在抽象类中定义工厂函数,规定子类实例化方式

复制代码
abstract class Shape {
  //factory constructor
  factory Shape(String type){
    if(type == 'circle') return Circle(10);
    if(type == 'square') return Square(10);
    throw 'can\'t create $type';
  }
  num get area;
}
复制代码

13. Dart中没有interface关键字,每个类都可以做接口

import 'shapes.dart';

class CircleMock implements Circle {
  num area;
  num radius;
}
复制代码
// 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(String who) => 'Hi $who, I\'m $_name';
}

// An implementation of the Person interface.
class Impostor implements Person {
  @override
  String get _name => this._name;

  @override
  String greet(String who) => 'Hi $who, do you know who I am?';
}

//polymorphism
String greetBob(Person person) => person.greet('Bob');

main() {
  print(greetBob(Person('chris')));
  print(greetBob(Impostor()));
}
复制代码

14. 同一对象级联操作

通过..符号可以在同一对象上级联操作。

querySelector('#confirm') // Get an object.
  ..text = 'Confirm' // Use its members.
  ..classes.add('important')
  ..onClick.listen((e) => window.alert('Confirmed!'));

15. 函数无需声明可能抛出的异常类型

16. 捕获异常

try {
  // ···
} on Exception catch (e) {
  print('Exception details:\n $e');
} catch (e, s) {
  print('Exception details:\n $e');
  print('Stack trace:\n $s');
}

17. 在运行时,Dart保留泛型类型信息

  var names = List<String>();
  names.addAll(['Seth', 'Kathy', 'Lars']);
  print(names is List<String>); // true

 

posted on   -赶鸭子上架-  阅读(1206)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示